我创建了一个 customCell UIButton
和UILabel
代码在这里:
ItemViewController.h:
@interface ItemViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSArray *arr;
IBOutlet ItemCustomCell *itemCell;
}
@property(nonatomic,retain)IBOutlet UITableView *tblItem;
ItemViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
ItemCustomCell *cell = (ItemCustomCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"ItemCustomCell" owner:self options:nil];
cell = itemCell;
}
cell.btnPlus.tag=indexPath.row;
[cell.btnPlus addTarget:self action:@selector(incrementValue:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)incrementValue:(UIButton *)btnAdd
{
NSLog(@"btn%d",btnAdd.tag);
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:btnAdd.tag inSection:0];
ItemCustomCell *cell = (ItemCustomCell*)[tblItem cellForRowAtIndexPath:indexPath];
cell.lblCount.text=[NSString stringWithFormat:@"%d",[cell.lblCount.text intValue]+1];
}
ItemCustomCell.h
@interface ItemCustomCell : UITableViewCell
{
}
@property(nonatomic,strong)IBOutlet UIButton *btnPlus;
@property(nonatomic,assign)IBOutlet UILabel *lblCount;
标签的默认值为 1。当我单击按钮时,它会显示下一个值。
当我向上或向下滚动时,tableView 标签值重置为 1。我在这里做错了什么?