ios - CustomCell 标签值不变

标签 ios objective-c cocoa-touch uitableview

我用 UIButtonUILabel 创建了一个 customCell

代码在这里:

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。我在这里做错了什么?

最佳答案

对于customCell,需要在Xib中指定它的复用标识和类名,加载到你的cell中复用:

    cell = [[[NSBundle mainBundle] loadNibNamed:@"ItemCustomCell" owner:self options:nil] lastObject];

编辑

最好使用 IBOutlet 和委托(delegate)在 CustomCell 中实现操作,然后使用标签。

//ItemCustomCell.h
@class ItemCustomCell;
@protolcol ItemCustomCellDelegate
  -(void) clickPlusButtonInsideCell:(ItemCustomCell *)cell;
@end
@interface ItemCustomCell
@property(weak, nonatomic) id<ItemCustomCellDelegate> delegate;
//Hookup with your view in Xib
@property (weak, nonatomic) IBOutlet UILabel *label;
-(IBACtion)clickPlusBut:(id)sender;
@end
//ItemCustomCell.m
-(IBACtion)clickPlusBut:(id)sender{
 [self.delegate clickPlusButtonInsideCell:self];
}

使用

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    ItemCustomCell *cell = (ItemCustomCell *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"ItemCustomCell" owner:self options:nil] lastObject];
    }
    cell.delegate = self;

    return cell;
}

-(void) clickPlusButtonInsideCell:(ItemCustomCell *)cell{
   cell.label.text = @"something";
}

关于ios - CustomCell 标签值不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18228037/

相关文章:

ios - 检测应用程序的首次运行

android - 如何将文本放入带有 flutter 的滑动器中?

ios - UITableview 自定义单元格在最后重新加载,卡顿,被切成两半,并且在滚动期间不重新加载

iphone - iOS系统如何不震动播放系统声音?

ios - 模仿 tableView :willBeginEditingRowAtIndexPath: on UIViewController 的 UITableViewControllers 行为

ios - 自定义 UICollectionViewLayout 动画问题

ios - 为什么我的 iCloud Drive AppFolder 在 MacOS 上可见,但在 iOS 上的 iCloud Drive App 中被禁用?

ios - 具有多个 animationDuration iOS 的图像序列动画

ios - 在 iOS 中渲染水彩画

ios - 在 UITextView 中获取选定的文本