iphone - UITableView 的 reloadRowsAtIndexPaths : (NSArray *) indexPaths failing to cause a reload unless you call it twice?

标签 iphone ios ipad uitableview reload

我有一个 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/

相关文章:

ios - 在 ios 上选择 opengl-es 的代码

iphone - 在没有导航 Controller 的情况下在 Storyboard 中的 UIViewController 之间切换

ios - 核心数据与 iCloud 同步随机崩溃

ios - 在 objective-c 中,您将如何扫描字符串数组以获得一组子字符串?

iphone - iphone 上 Bootstrap 响应式设计中的页面宽度

ios - 总是将除一个对象之外的两个对象添加到数组中(否则错误)

objective-c - 手势识别器的正确 MVC 模式

ios - 为 iOS5 和 Storyboard更新 MGSplitViewController 的已知努力?

ios - Xcode Profile和物理设备离线问题

iPhone SDK : Forcing landscape mode for a single part of an app