强制 UITableView 为所有单元格调用 cellForRowAtIndexPath:
我在使用 UITableView 的 reloadData
方法时遇到一些问题。我发现它仅在添加或删除新单元格时调用 cellForRowAtIndexPath 。
示例:我有五个单元格,每个单元格包含五个字符串。如果我添加一个新单元格并调用 reloadData
,表格就会更新并且我会看到它。然而,如果我进入五个单元格之一,添加一个新字符串,然后返回并调用 reloadData
,则不会调用任何表视图的委托方法。
我的问题:是否可以强制表视图完全重新加载所有可见单元格中的数据?
I am having some trouble with UITableView's reloadData
method. I have found that it only calls cellForRowAtIndexPath if there are new cells being added or taken away.
Example: I have five cells, each containing five strings. If I add a new cell and call reloadData
, the table is updated and I see it. Yet if I go into one of the five cells, add a new string, then return and call reloadData
, none of the table view's delegate methods is called.
My question: Is it possible to force the table view to completely reload the data in all of its visible cells?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现了问题 - 我的单元格自定义代码位于 if(cell == nil) 块中,因此由于单元格被回收,因此它们没有被更改。从该块中取出我的自定义代码解决了问题。
I found the problem- I had my cell customization code in the
if(cell == nil)
block, so because the cells were being recycled, they weren't being changed. Taking my customization code out of that block fixed the problem.我发现重新加载部分可以更轻松地重新加载数据。如果您只有一个部分,您可以尝试:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
I've found that reloading sections reloads data more readily. If you just have one section you can try:
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
您可以尝试此操作
或
将其放入
commitEditingStyle
方法中。You can try this
or
put this in
commitEditingStyle
method.好吧,表格视图只会在可见单元格上调用
cellForRowAtIndexPath
,但如果你确实调用reloadData
它会这样做,所以这里还有其他事情发生,我不会确定它是什么。Well, the table view's only going to call
cellForRowAtIndexPath
on the visible cells but if you do callreloadData
it will do that so there's something else going on here and I'm not sure what it is.即使您的表视图有 50 行,也只会存在一次可见的单元格数量。这就是reuseIdentifier 背后的整个故事。因此,强制“所有细胞”并不存在。如果出现新单元格,则会动态加载数据。
更改单元格的唯一方法是更改由 dataSource 方法 cellForRowAtIndexPath 传递的数据。
Even if your tableview has 50 rows, there only will exist as much cells as can be visible at one time. That's the whole story behind the reuseIdentifier. So forcing 'all the cells' doesn't exist. If a new cell appears, the data is loaded dynamically.
The only way to change a cell is to change the data that is delivered by the dataSource method cellForRowAtIndexPath.
不要将代码移出
if (cell == nil)
块。相反,为您正在创建的单元格创建一个代表性标识符;尝试并确保标识符中引用了所有单元格的内容。例如,如果显示 3 个数字,请确保以唯一的方式将这三个数字包含在标识符中,从而仅引用具有此类内容的单元格。假设您的类中有三个 NSArray 属性:array1、array2 和 array3,它们的 int 值包含在 NSNumber 对象内。你想使用这些 NSArray 来填充 UITableView,这就是我要做的:
Do not take your code out of the
if (cell == nil)
block. Instead, create a representative identifier for the cell you're making; try and make sure that all of the cell's content is referred to in the identifier. For example, if you have 3 numbers showing, make sure to have those three numbers in the identifier in a unique way that would only refer to a cell that has such content.Let's say you have three NSArray properties in your class, array1, array2, and array3 that have int values wrapped inside of NSNumber objects. You want to use those NSArrays to fill a UITableView, this is what I'd do: