TableView reloadData与beginUpdates& endUpdates [英] TableView reloadData vs. beginUpdates & endUpdates

查看:199
本文介绍了TableView reloadData与beginUpdates& endUpdates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于更新我的TableView的棘手问题,我使用不同的更新方法得到不同的结果,让我解释一下:

I got a tricky problem regarding updating my TableView, i get different results using different methods of updating it, let me explain:

情况1:
我使用 [tbl reloadData]; 其中 tbl 是我的TableView,用于更新TableView - 按预期工作。

Situation 1: I use [tbl reloadData]; where tbl is my TableView, to update the TableView - works as intended.

情况2:
我使用:

Situation 2: I use:

[tbl beginUpdates];
[tbl reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight];
[tbl endUpdates];

其中 tbl 是我的TableView,并且 indexPaths 是一个包含TableView中存在的所有indexPath的数组。现在数组很好,它包含所有正确的indexPaths(双重和三重检查)但由于某种原因 - 这不能按预期工作。

Where tbl is my TableView, and indexPaths is an array containing all the indexPaths present in the TableView. Now the array is fine, it contains all the correct indexPaths (double and triple checked) but for some reason - this does not work as intended.

现在我意识到这一点是一个XY问题(我要求Y,但我的问题是X,因为我认为解决Y会解决X),这只是因为我觉得解释X(上述问题的后果)很简单,有点复杂,所以如果可能的话,我宁愿避免这种情况。

Now I realize that this is an X-Y problem (where I ask for Y but my problem is really X because I think solving Y will solve X) and thats only because I feel it's a bit complicated explaining X (the consequence of said above problem) in an easy way, so I'd rather refrain from that if possible.

所以,问题是:两种更新方式之间是否存在差异TableView(当然除了动画位)或者我应该怀疑问题出在其他地方吗?

So, down to my question: Is there a difference between the two ways of updating the TableView (aside from the animation bit of course) or should I suspect the problem to lay elsewhere?

编辑:
好​​的,我将尝试解释症状是什么:

Okay, I'll try to explain what the symptoms are:

cellForRowAtIndexPath -method我添加一个按钮具有指定标记的每个单元格等于单元格的indexPath行,如下所示:

In the cellForRowAtIndexPath-method I add a button to each cell with an assigned tag which is equal to the cell's indexPath row, like such:

btn.tag = indexPath.row;

我这样做的原因是我可以识别每个按钮,因为它们都调用相同的功能: / p>

The reason I do this is so I can identify each button as they all call the same function:

- (void)btnPressed:(id)sender

当我然后更新单元格 - 因为单元格中的某些值已经改变 - 情境1使一切正常,情况2然而 - 混合标记,以便下次其中一个按下按钮,它们不再具有正确的标签。

When I then update the cells - because some values in the cells have changed - Situation 1 makes everything work fine, Situation 2 however - mixes up the tags so the next time one of the buttons are pressed, they no longer have the correct tags.

混音对我来说确实是随机的,但随机化的发生方式也不同,这取决于我先按哪个单元格按钮。我希望这能澄清我的问题。

The mix-up does appear random to me, but the randomization occurs differently depending on which cells button I press first. I hope this clarifies my problem.

推荐答案

来自 UITableView 文档


beginUpdates

开始一系列方法调用,插入,删除或
选择行和部分接收器。

beginUpdates
Begin a series of method calls that insert, delete, or select rows and sections of the receiver.

这意味着,除非您要插入,删除或选择,否则不应使用此接收器。你没有做这些。

That means, you should not use this unless you are inserting, deleting or selecting. You are doing neither of these.

另外,你应该用 endUpdates <结束 beginUpdates / code>,而不是 reloadData 。文档:

Also, you should end beginUpdates with endUpdates, not reloadData. Documentation:


这组方法必须以调用endUpdates结束。

This group of methods must conclude with an invocation of endUpdates.

这篇关于TableView reloadData与beginUpdates&amp; endUpdates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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