我正在尝试更新可重用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];发布于 2013-02-09 02:41:42
这是不起作用的,因为您使用创建的第一个单元格更改了按钮的标记。当您将一个要重用的单元格出队时,它的按钮的标记已经与以前相比发生了变化,因此它不再是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;
}
@endhttps://stackoverflow.com/questions/14778656
复制相似问题