ios - UITableView 中批量行插入/删除的顺序是什么?

标签 ios cocoa-touch uitableview

UITableView 允许您使用 beginUpdatesendUpdates 进行批量编辑操作。

我的问题是:我需要先知道它是删除还是插入?或者我可以通过 beginUpdates 之前的索引路径引用所有内容,它会神奇地起作用吗?

假设我有一张 table :

A (currently index path 0,0)
B (0,1)
C (0,2)
D (0,3)
E (0,4)
F (0,5)

我想把它变成:

A (0,0)
C (0,1)
D (0,2)
H (0,3)
E (0,4)
F (0,5)

因此,我删除了 B(在 0,1 处)并插入了 H(在 D 之后插入 - 在删除之前的 0,4 处,或之后的 0,3 处)。

那么,在我的开始/结束更新调用之间,哪些会起作用?

  1. deleteRowsAtIndexPaths:0,1,然后是 insertRowsAtIndexPaths: 0,4
  2. deleteRowsAtIndexPaths: 0,1,后跟 insertRowsAtIndexPaths: 0,3
  3. insertRowsAtIndexPaths:0,4,然后是 deleteRowsAtIndexPaths:0,1
  4. insertRowsAtIndexPaths:0,3,然后是 deleteRowsAtIndexPaths:0,1

最佳答案

与此相关的 Apple 文档位于 Ordering of Operations and Index Paths 下.

Deletion and reloading operations within an animation block specify which rows and sections in the original table should be removed or reloaded; insertions specify which rows and sections should be added to the resulting table. The index paths used to identify sections and rows follow this model.

因此 TableView 将首先执行任何删除或更新操作,其索引路径引用原始表内容中的索引路径。然后执行插入,这些索引路径引用删除之后的索引路径。

因此理论上您的数字“2”选项应该是您想要的那个。

关于ios - UITableView 中批量行插入/删除的顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6927221/

相关文章:

ios - 如何在不卡住主线程的情况下计算 NSAttributedString 的高度

ios - 带 Collection View 的动态布局 iOS

ios - UIPageViewController:如何添加非分页工具栏或其他 "overlay"元素?

ios - NSData 导致泄漏

ios - 如何在 objective-c 中绘制半圆形图层的切片

ios - 将按钮添加到单元格

ios - React Native Text 组件不会在 View 组件内居中

objective-c - KVO可以用来观察一个全局变量吗?

ios - 自定义单元格中按钮的索引

ios - 具有多个部分的 TableView