为UITableView调用beginUpdates / endUpdates有什么好处,而不是这样做? [英] What is the benefit of calling beginUpdates/endUpdates for a UITableView as opposed to not doing so?

查看:203
本文介绍了为UITableView调用beginUpdates / endUpdates有什么好处,而不是这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组数组,我正用于表视图的数据源。在一个时间实例中,我可能不得不对此数据结构进行一些复杂的修改。 (例如,我可能需要做的一系列操作是:在这里删除一行,在那里插入一行,在此插入一个部分,删除另一行,插入另一行,删除另一行,插入另一部分 - 你得到的如果对于序列中的每个操作,我只更新数据源,然后立即对表视图进行相应的更新,这很容易做到。换句话说,伪代码看起来像:

I have an array of arrays that I'm using for the data source of a table view. In one instance of time, I may have to do some complicated modifications to this data structure. (For example, a sequence of operations I may need to do is: delete a row here, insert a row there, insert a section here, delete another row, insert another row, delete another row, insert another section--you get the idea.) This is easy to do if, for each operation in the sequence, I just update the data source and then do the corresponding update for the table view immediately. In other words, the pseudocode will look like:

[arrayOfArrays updateForOperation1];
[tableView updateForOperation1];

[arrayOfArrays updateForOperation2];
[tableView updateForOperation2];

[arrayOfArrays updateForOperation3];
[tableView updateForOperation3];

[arrayOfArrays updateForOperation4];
[tableView updateForOperation4];

// Etc.

但是,如果我要围绕这些操作在beginUpdates / endUpdates块中,此代码不再起作用。要了解原因,请先从空表视图开始,然后在第一部分的开头依次插入四行。这是伪代码:

However, if I were to surround these operations in a beginUpdates/endUpdates "block," this code does not work anymore. To see why, imaging beginning with an empty table view and inserting four rows in turn at the beginning of the first section. Here's the pseudocode:

[tableView beginUpdates];

[arrayOfArrays insertRowAtIndex:0];
[tableView insertRowAtIndexPath:[row 0, section 0]];

[arrayOfArrays insertRowAtIndex:0];
[tableView insertRowAtIndexPath:[row 0, section 0]];

[arrayOfArrays insertRowAtIndex:0];
[tableView insertRowAtIndexPath:[row 0, section 0]];

[arrayOfArrays insertRowAtIndex:0];
[tableView insertRowAtIndexPath:[row 0, section 0]];

[tableView endUpdates];

当调用endUpdates时,表视图发现你正在插入四行全部在第0行碰撞!

When endUpdates gets called, the table view discovers that you are inserting four rows all colliding at row 0!

如果我们真的想保留beginUpdates / endUpdates代码的一部分,我们必须做一些复杂的事情。 (1)我们在不更新表视图的情况下对数据源进行所有更新。 (2)我们弄清楚在所有更新之前数据源的各部分如何在所有更新之后映射到数据源的各个部分,以确定我们需要为表视图执行哪些更新。 (3)最后,更新表视图。伪代码看起来像这样,以完成我们在前面的例子中尝试做的事情:

If we REALLY want to keep the beginUpdates/endUpdates part of the code, we have to do something complicated. (1) We do all the updates to the data source without updating the table view as we go along. (2) We figure out how the parts of the data source before all the updates maps to the parts of the data source after all the updates to figure out what updates we need to perform for the table view. (3) Finally, update the table view. The pseudocode looks something like this to accomplish what we were trying to do in the previous example:

oldArrayOfArrays = [self recordStateOfArrayOfArrays];

// Step 1:
[arrayOfArrays insertRowAtIndex:0];
[arrayOfArrays insertRowAtIndex:0];
[arrayOfArrays insertRowAtIndex:0];
[arrayOfArrays insertRowAtIndex:0];

// Step 2:
// Comparing the old and new version of arrayOfArrays,
// we find we need to insert these index paths:
// @[[row 0, section 0],
//   [row 1, section 0],
//   [row 2, section 0],
//   [row 3, section 0]];
indexPathsToInsert = [self compareOldAndNewArrayOfArraysToGetIndexPathsToInsert];

// Step 3:

[tableView beginUpdates];

for (indexPath in indexPathsToInsert) {
    [tableView insertIndexPath:indexPath];
}

[tableView endUpdates];

为什么这一切都是为了beginUpdates / endUpdates?文档说使用 beginUpdates endUpdates 做两件事:

Why do all this for the sake of beginUpdates/endUpdates? The documentation says to use beginUpdates and endUpdates to do two things:


  1. Animate一堆插入,删除和其他操作同时

  2. 如果你没有在这个块里面进行插入,删除和选择调用,那么表属性如此因为行计数可能无效。 (这究竟是什么意思呢?)

但是,如果我不使用beginUpdates / endUpdates,表视图看起来像它同时动画了各种变化,我认为表视图的内部一致性没有被破坏。那么使用beginUpdates / endUpdates进行复杂方法的好处是什么?

However, if I don't use beginUpdates/endUpdates, the table view looks like it's animating the various changes simultaneously, and I don't think the table view's internal consistency is being corrupted. So what is the benefit of doing the complicated approach with the beginUpdates/endUpdates?

推荐答案

每次添加/删除表项时调用 tableView:numberOfRowsInSection:方法 - 除非用开始 / endUpdate包围这些调用。如果您的数组和表视图项不同步,没有开始/结束调用将抛出异常。

Every time you add/remove a table item the tableView:numberOfRowsInSection: method is called - unless you surround these calls with begin/endUpdate. If your array and table view items are out of sync, without the begin/end calls an exception will be thrown.

这篇关于为UITableView调用beginUpdates / endUpdates有什么好处,而不是这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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