强制UITableView转储所有可重复使用的单元格

Joh*_*ith 5 iphone cocoa-touch uitableview

我有一个UITableView,我通过它设置了背景色

UIView *myView = [[UIView alloc] init];
if ((indexPath.row % 2) == 0)
    myView.backgroundColor = [UIColor greenColor];
else
    myView.backgroundColor = [UIColor whiteColor];

cell.backgroundView = myView;
[myView release];
Run Code Online (Sandbox Code Playgroud)

我发现的问题是,当我编辑表(通过setEditing:YES ...)时,某些颜色不变的单元格彼此相邻。如何强制UITableView完全重绘。reloadData做得不好。

是否有深层清洁重绘?

Jac*_*kin 4

我之前也遇到过这个问题,所以我分享一下我是如何解决的:

您可以使用布尔标志(假设它被称为needsRefresh)来控制单元格创建的行为-cellForRowAtIndexPath

一个例子:

- (UITableViewCell*) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath {
    UITableViewCell *cell = [tableView dequeueResuableCellWithIdentifier:SOME_ID];
    if(!cell || needsRefresh) {
       cell = [[[UITableViewCell alloc] init....] autorelease];
    }
    //.....
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

因此,当您需要硬重新加载时,请将needsRefresh标志设置为YES。简单如痘痘。

  • 我想知道在执行所有必需的重新加载后,您在哪里将 needRefresh 标志设置回 NO...此解决方案是否应该通过某种单元格跟踪来补充,在更新所有可见单元格后将其设置回 false? (2认同)