是否可以将tableView.moveRow用于具有动态节数的表



我正在尝试创建一个具有动态节数的表。这会导致 tableView.moveRow 崩溃,即使我在调用数据源之前已经更新了它。

例如(

A B

| 在列表中--- A 和 B。节数 = 1,节中的行数 = 2

一 |B 在第 0 节中--- A,在第 1 节中 B。节数 = 2,每节中的行数 = 1

我在-[UITableView _endCellAnimationsWithContext:]中不断遇到断言失败,我想知道这是否超出了 UITableView 的范围,或者我的代码中是否有错误?

我的代码是

let (fromPath, toPath) = viewModel.moveItem(A) // Creates new section and moves A to the new section below
dprint("Move (A.name) from (fromPath) to (toPath)")
tableView.moveRow(at: fromPath, to: toPath)

输出为

将 A 从 [0, 0]

移动到 [1, 0]

2018-01-23 13:59:45.705931+0100 MyApp[44553:638288] *** 断言失败 -[UITableView _endCellAnimationsWithContext:],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.33.6/UITableView.m:1826

使用 tableView.beginUpdates()tableView.endUpdates()tableView更改括起来。

此外,您还必须注意创建新部分,而更改数据模型将在数据中创建一个新部分,您必须告诉tableView在移动行之前插入一个新部分:

let (fromPath, toPath) = viewModel.moveItem(A) // Creates new section and moves A to the new section below
dprint("Move (A.name) from (fromPath) to (toPath)")
tableView.beginUpdates()
if newSectionWasCreated {
    tableView.insertSections([toPath.section], with: .automatic)
}
tableView.moveRow(at: fromPath, to: toPath)
tableView.endUpdates()

您必须弄清楚如何根据模型确定自己newSectionWasCreated

最新更新