【发布时间】:2014-06-30 23:39:28
【问题描述】:
在uicollectionviewcell类中初始化两个uibutton和一个uitextfield
然后点击单元格内的按钮获得索引路径,现在停留在这一点上,如何在collectionviewcell内的按钮点击时更新文本字段中的文本
按钮是 (+) 加号和 (-) 减号,实际上我必须从用户那里远程输入他们对我的收藏视图中显示的任何产品的需求量。
这是我正在做的,但没有成功
自定义集合视图类
@interface MyCell : UICollectionViewCell
@property (retain, nonatomic) UIButton *btn1;
@property (retain, nonatomic) UIButton *btn2;
@property (retain, nonatomic) UITextField *txt1;
@end
实施
@implementation MyCell
@synthesize btn1, btn2, txt1;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
CGRect btn1Rect = CGRectMake(190, 230 , 35 , 35);
btn1 = [[UIButton alloc] initWithFrame:btn1Rect];
btn1.tag=11;
[btn1 setBackgroundImage:[UIImage imageNamed:@"BtnPlus.png"] forState:UIControlStateNormal];
//btn1.layer.borderWidth = 1.0f;
CGRect btn2Rect = CGRectMake(105, 230 , 35 , 35);
btn2 = [[UIButton alloc] initWithFrame:btn2Rect];
btn2.tag=12;
[btn2 setBackgroundImage:[UIImage imageNamed:@"BtnMinus.png"] forState:UIControlStateNormal];
//btn2.layer.borderWidth = 1.0f;
CGRect txt1Rect = CGRectMake(143, 230 , 45 , 30);
txt1 = [[UITextField alloc] initWithFrame:txt1Rect];
txt1.tag=13;
txt1.font=[UIFont fontWithName:@"Superclarendon" size:18];
txt1.textAlignment = NSTextAlignmentCenter;
txt1.textColor = [UIColor blueColor];
//txt1.layer.borderWidth = 1.0f;
}
return self;
}
@end
在主类中
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellID" forIndexPath:indexPath];
[[cell btn1] addTarget:self action:@selector(btnPlus:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.btn1];
[[cell btn2] addTarget:self action:@selector(btnMinus:event:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.btn2];
[cell.contentView addSubview:cell.txt1];
return cell;
}
处理程序方法代码是
-(void)btnPlus:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:myCollection];
NSIndexPath *indexPath = [myCollection indexPathForItemAtPoint: currentTouchPosition];
NSLog(@"%@", indexPath);
}
这就是我所做的一切,,,现在请帮助我解决或找出我的问题。
只想在特定单元格的文本字段中输入,然后保存此文本……。
【问题讨论】:
标签: ios iphone objective-c ipad uicollectionview