我已经创建了一个自定义的UITableViewCell,在其中我向它的contentView添加了一堆动态加载的按钮。这些文件显示正确。
当控制加载哪些按钮的数据发生变化时,我调用了reloadData,可以看到调用了prepareForReuse:
- (void) prepareForReuse {
NSLog(@"prep for reuse");
[self clearButtons];
}
- (void) clearButtons {
NSLog(@"clearButtons called");
self.buttons;
for (UIView* v in buttons) {
NSLog(@"clearing a button.");
[v removeFromSuperview];
[v dealloc];
}
buttons = [[NSMutableArray alloc] init];
}不过,这些按钮不会从superview中删除,即“清除按钮”。永远不会记录消息。
然后,我在tableView:cellForRowAtIndexPath中添加了对clearButtons的显式调用
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell clearButtons];这一次,我可以看到“清除按钮”。已记录,并且所有内容都正确显示。
怎么回事?当从prepareForReuse调用时,为什么按钮没有被移除?
发布于 2013-07-26 09:49:40
必须在创建单元格时指定重用标识符,否则UITableView将不会调用您的“prepareForReuse”方法。
根据文档,您还必须调用超级prepareForReuse;
发布于 2010-10-19 11:32:50
这可能并不相关,但在createButtons中有几件事需要修复:
在初始化新的buttons之前,也要使用[v release]而不是
buttons。我还想问一下,如果你删除了除NSLog之外的所有东西,它能正常工作吗?
https://stackoverflow.com/questions/3964453
复制相似问题