UITableView的reloadRowsAtIndexPaths:(NSArray*)indexPaths除非你调用它两次,否则无法重新加载?

Wil*_*sch 7 iphone reload uitableview ipad ios

我有一个UITableViewController管理iPad应用程序中的UITableView对象.表视图与其他对象的相当复杂的星座相关联.当我要求它按如下方式重新加载时,我遇到了问题:

//indexPath is an NSIndexPath with indexes 0 and 0.  At the moment, this is the only cell in the table view.
NSArray *array = [NSArray arrayWithObject:indexPath]; 
[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone]; 
Run Code Online (Sandbox Code Playgroud)

问题是该行没有重新加载.永远不会回调cellForRowAtIndexPath.

疯狂的是,如果我两次调用reloadRowsAtIndexPaths,第二次调用会触发重新加载:

//indexPath is an NSIndexPath with indexes 0 and 0.  At the moment, this is the only cell in the table view.
NSArray *array = [NSArray arrayWithObject:indexPath]; 
[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];   // does not reload
[self.tableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];   // does reload
Run Code Online (Sandbox Code Playgroud)

我想知道是否有其他人遇到过这样的错误,如果是这样,原因是什么?

ama*_*ttn 6

reloadRowsAtIndexPaths:... 仅在调用以下内容之间起作用:

- (void)beginUpdates;
- (void)endUpdates;
Run Code Online (Sandbox Code Playgroud)

除此之外,行为是不确定的.(正如你所发现的,相当不可靠).

编辑:引用"iPhone OS表视图编程指南"的相关部分:

要为行和节的批量插入和删除设置动画,请在连续调用beginUpdates和endUpdates定义的动画块中调用插入和删除方法.如果不在此块中调用插入和删除方法,则行和节索引可能无效.beginUpdates ... endUpdates块不可嵌套.

在块结束时 - 也就是说,在endUpdates返回之后 - 表视图查询其数据源并像往常一样委托行和段数据.因此,应更新支持表视图的集合对象以反映新的或删除的行或节.

reloadSections:withRowAnimation:和reloadRowsAtIndexPaths:withRowAnimation:方法,在iPhone OS 3.0中引入,与上面讨论的方法有关.它们允许您请求表视图重新加载特定部分和行的数据,而不是通过调用reloadData来加载整个可见表视图.

  • 去年这个问题害了我一周……它确实说了,但它**隐藏**在表视图编程指南中,而不是参考文档中。 (2认同)