【问题标题】:Combining moveSection:toSection, insertSections:, and deleteSections: in a UITableView update block在 UITableView 更新块中结合 moveSection:toSection、insertSections: 和 deleteSections:
【发布时间】:2012-10-27 00:35:17
【问题描述】:

UITableView documentation 表示可以在 beginUpdates-endUpdates 块中组合对 moveSection:toSection:insertSections:withRowAnimation:deleteSections:withRowAnimation: 的调用。 Table View Programming Guide 中名为 Batch Insertion, Deletion, and Reloading of Rows and Sections 的部分还解释了当在更新块中混合插入和删除时,无论方法调用的顺序如何,表视图都会在插入之前先进行删除。

我的问题是,当对moveSection:toSection: 的调用与对insertSections:deleteSections: 的调用结合使用时,表格视图的移动顺序是什么?或者,fromSectiontoSection 是指删除之前、删除和插入之间还是插入之后的节索引?

【问题讨论】:

    标签: ios uitableview


    【解决方案1】:

    在尝试了一些代码之后,似乎在beginUpdates-endUpdates 块中考虑一批更改的最佳方式是应用任何更新之前的部分顺序和顺序应用所有更新后的部分。如果 U 表示更新块之前的顺序,而 V 表示更新块之后的顺序,则deleteSections: 调用中使用的节索引和@987654324 中的源索引@ 是 U 中的索引,insertSections: 中使用的部分索引和 moveSection:toSection: 调用中的目标索引是 V 中的索引。

    因此,例如,为更改设置动画:

    0 C -> A 0 1 F H 1 2 H C 2 3 K K 3

    你可以使用:

    [tableView deleteSections:@[1] withRowAnimation:UITableViewRowAnimationAutomatic]; // delete F
    [tableView insertSections:@[0] withRowAnimation:UITableViewRowAnimationAutomatic]; // insert A
    [tableView moveSection:2 toSection:1]; // move H
    [tableView moveSection:0 toSection:2]; // move C (implied by other changes so not necessary)
    

    如果所做的其他更改暗示了某些移动,则无需明确进行。例如将 A 部分旋转到表格底部:

    0 A -> B 0 1 B C 1 2 C D 2 3 D A 3

    以下在逻辑上是等价的:

    [tableView moveSection:0 toSection:3];
    

    [tableView moveSection:1 toSection:0];
    [tableView moveSection:2 toSection:1];
    [tableView moveSection:3 toSection:2];
    

    但是它们的动画略有不同。在第一种情况下,A 部分在其他部分上向下移动,而在第二种情况下,向上移动的三个部分在 A 部分上设置动画。

    最后,似乎在为一组更改设置动画时,其中一些部分向下移动,另一些部分向上移动,不移动的部分位于底部,向下移动的部分位于其上方,而向上移动的部分位于顶部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 2019-10-29
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多