UITableView的reloadRowsAtIndexPaths:(NSArray *)indexPaths无法导致重新加载,除非你调用它两次? [英] UITableView's reloadRowsAtIndexPaths: (NSArray *) indexPaths failing to cause a reload unless you call it twice?

查看:108
本文介绍了UITableView的reloadRowsAtIndexPaths:(NSArray *)indexPaths无法导致重新加载,除非你调用它两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UITableViewController managing a UITableView object in an iPad app. The table view is tied in with a rather complicated constellation of other objects. I am having a problem when I ask it to reload a row as follows:

//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。

The problem is that the row does not reload. There is never a callback to cellForRowAtIndexPath.

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

The crazy thing is that if I call reloadRowsAtIndexPaths twice, the second call does trigger the reload:

//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

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

I am wondering if anyone else has ever encountered a bug like this, and if so, what was the cause?

推荐答案

reloadRowsAtIndexPaths:... 仅在调用之间有效:

- (void)beginUpdates;
- (void)endUpdates;

除此之外,行为未定义。 (正如你所发现的那样,相当不可靠)。

Outside of that, behavior is undefined. (and as you've discovered, fairly unreliable).

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

quoting relevant part of "Table View Programming Guide for iPhone OS":


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

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.

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

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.

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

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.

这篇关于UITableView的reloadRowsAtIndexPaths:(NSArray *)indexPaths无法导致重新加载,除非你调用它两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆