Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 具有prepareForReuse的UITableViewCell不工作_Iphone_Uitableview - Fatal编程技术网

Iphone 具有prepareForReuse的UITableViewCell不工作

Iphone 具有prepareForReuse的UITableViewCell不工作,iphone,uitableview,Iphone,Uitableview,我已经创建了一个自定义的UITableViewCell,在这里我将一组动态加载的按钮添加到它的contentView。这些显示正确 当控制加载哪些按钮的数据发生变化,我调用reloadData时,我可以看到调用了prepareforeuse: - (void) prepareForReuse { NSLog(@"prep for reuse"); [self clearButtons]; } - (void) clearButtons { NSLog(@"clearBu

我已经创建了一个自定义的
UITableViewCell
,在这里我将一组动态加载的按钮添加到它的
contentView
。这些显示正确

当控制加载哪些按钮的数据发生变化,我调用
reloadData
时,我可以看到调用了
prepareforeuse

- (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:cellforrowatinexpath
中添加了对
clearButtons
的显式调用:

CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

[cell clearButtons];
这一次,我可以看到“清除按钮”被记录下来,所有内容都正确显示


有什么好处?为什么从prepareForReuse调用时没有删除按钮?

这可能与此无关,但createButtons中有几件事需要解决:

  • 使用
    [v release]
    而不是
    [v dealloc]
  • 在初始化新按钮之前,也释放
    按钮

  • 我还想问,如果您删除除NSLog之外的所有内容,它是否有效?

    它可能与此无关,但您应该在createButtons中修复的内容很少:

  • 使用
    [v release]
    而不是
    [v dealloc]
  • 在初始化新按钮之前,也释放
    按钮

  • 我还想问,如果您删除除NSLog之外的所有内容,它是否有效?

    您必须在创建单元格时指定重用标识符,否则UITableView将不会调用您的“prepareForReuse”方法


    根据文档,您还必须调用[super prepareforeuse]

    创建单元格时必须指定重用标识符,否则UITableView将不会调用“prepareForReuse”方法


    根据文档,您还必须调用[super prepareforeuse]

    我对我的手机进行了分类,并且预习很好。我认为问题可能出在方法clearButtons上。我对我的单元格进行了子分类,prepareForReuse运行良好。我认为问题可能在于方法-clearButtons。