我有一个 UITableViewController 在 iPad 应用程序中管理一个 UITableView 对象。表格 View 与其他对象的相当复杂的星座联系在一起。当我要求它重新加载一行时遇到问题,如下所示:
//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];
问题是该行没有重新加载。永远不会回调 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
我想知道是否还有其他人遇到过这样的错误,如果遇到过,原因是什么?
最佳答案
reloadRowsAtIndexPaths:...
仅在以下调用之间有效:
- (void)beginUpdates;
- (void)endUpdates;
除此之外,行为是未定义的。 (正如您所发现的,相当不可靠)。
编辑:引用“iPhone OS Table View Programming Guide”的相关部分:
To animate a batch insertion and deletion of rows and sections, call the insertion and deletion methods within an animation block defined by successive calls to beginUpdates and endUpdates. If you don’t call the insertion and deletion methods within this block, row and section indexes may be invalid. beginUpdates...endUpdates blocks are not nestable.
At the conclusion of a block—that is, after endUpdates returns—the table view queries its data source and delegate as usual for row and section data. Thus the collection objects backing the table view should be updated to reflect the new or removed rows or sections.
The reloadSections:withRowAnimation: and reloadRowsAtIndexPaths:withRowAnimation: methods, which were introduced in iPhone OS 3.0, are related to the methods discussed above. They allow you to request the table view to reload the data for specific sections and rows instead of loading the entire visible table view by calling reloadData.
关于iphone - UITableView 的 reloadRowsAtIndexPaths : (NSArray *) indexPaths failing to cause a reload unless you call it twice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557504/