Iphone 可重用UITableViewCells的子按钮不允许我更新标记

Iphone 可重用UITableViewCells的子按钮不允许我更新标记,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我正在尝试更新可重用UITableViewCell中的按钮标记。对于前5-8个单元格,设置标签没有问题,因为据我所知,这些单元格尚未“重用”。一旦UI必须重用单元格,它就不再允许我更改标记或设置按钮的标记。我错过了什么 UITableViewCell *cell =[tblPlaces dequeueReusableCellWithIdentifier:@"mainTableViewCell"]; if (cell == nil) { cell = [[UITableViewCell al

我正在尝试更新可重用UITableViewCell中的按钮标记。对于前5-8个单元格,设置标签没有问题,因为据我所知,这些单元格尚未“重用”。一旦UI必须重用单元格,它就不再允许我更改标记或设置按钮的标记。我错过了什么

UITableViewCell *cell =[tblPlaces dequeueReusableCellWithIdentifier:@"mainTableViewCell"];

if (cell == nil) {
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"mainTableViewCell"];
}

[[cell viewWithTag:107] setTag:indexPath.section];

这不起作用,因为您正在使用创建的第一个单元格更改按钮的标记。当您将要重用的单元格出列时,它的button标记已经从以前更改过,因此它不再是107,而是旧索引的内容

我会考虑子类UITableViewCell,并将按钮添加为子类的属性。这样您就可以直接访问它,而不需要使用标签

编辑:

下面是一个非常简单的示例,说明了您真正需要做的所有事情:

@interface MyTableViewCell : UITableViewCell
@property (nonatomic, retain) UIButton *myButton;
@end

@implementation MyTableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Create and add your button in here, or set it equal to the one you create in Interface Builder
    }
    return self;
}

@end

你把这个叫哪里?就在你代码的任何地方?或者在您的CellForRowatineXpath中?它在我的CellForRowatineXpath函数中。如果有帮助的话,该单元格实际上来自故事板中的UITableView。我正在以我相信的正确方式初始化。我还编辑了上面的代码块。我知道我遗漏了一件小事。您是否有任何明确的例子,可以将UITableViewCell子类化用于此特定目的?谢谢!实际上,子类化对我来说非常有用。它给了我完全的控制,而不依赖于一个标签值进行操作。