可折叠部分:[断言] 无法确定 preReloadFirstVisibleRow (0) 的新全局行索引



我在UITableViewController中实现了可折叠的部分标题。

以下是我确定每个部分显示多少行的方法:

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return self.sections[section].isCollapsed ? 0 : self.sections[section].items.count
}

有一个结构体保存部分信息,带有布尔值表示"isCollapsed"。

以下是我切换其状态的方式:

private func getSectionsNeedReload(_ section: Int) -> [Int]
{
var sectionsToReload: [Int] = [section]
let toggleSelectedSection = !sections[section].isCollapsed
// Toggle collapse
self.sections[section].isCollapsed = toggleSelectedSection
if self.previouslyOpenSection != -1 && section != self.previouslyOpenSection
{
self.sections[self.previouslyOpenSection].isCollapsed = !self.sections[self.previouslyOpenSection].isCollapsed
sectionsToReload.append(self.previouslyOpenSection)
self.previouslyOpenSection = section
}
else if section == self.previouslyOpenSection
{
self.previouslyOpenSection = -1
}
else
{
self.previouslyOpenSection = section
}
return sectionsToReload
}

internal func toggleSection(_ header: CollapsibleTableViewHeader, section: Int)
{
let sectionsNeedReload = getSectionsNeedReload(section)
self.tableView.beginUpdates()
self.tableView.reloadSections(IndexSet(sectionsNeedReload), with: .automatic)
self.tableView.endUpdates()
}

一切都运行良好,动画效果很好,但是在控制台中折叠扩展部分时,我得到这个[断言]:

[断言] 无法确定预重新加载第一个可见行的新全局行索引 (0(

无论它是相同的打开部分,关闭(折叠(,还是我打开另一个部分并"自动关闭"先前打开的部分,都会发生这种情况。

我没有对数据做任何事情;这是持久的。

谁能帮忙解释一下缺少什么?谢谢

为了让tableView在重新加载行等时知道它在哪里,它试图找到一个用作参考的"锚行"。这称为preReloadFirstVisibleRow。由于此表视图在某个时候可能没有任何可见行,因为所有部分都被折叠,因此表视图会因为找不到锚点而感到困惑。然后它将重置为顶部。

解决方案:为每个折叠的组添加一个 0 高度的行。这样,即使一个部分被折叠,仍然存在一行(尽管高度为 0px(。然后,表视图始终有一些可以挂钩的内容作为参考。如果行计数为 0,您将通过在numberOfRowsInSection中添加一行来看到这一点,并通过确保在需要indexPath.row之前返回 phatom 单元格值来处理任何进一步的indexPath.row调用(如果datasource.visibleRows为 0(。

在代码中演示更容易:

func numberOfSections(in tableView: UITableView) -> Int {
return datasource.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource[section].visibleRows.count == 0 ? 1 : datasource[section].visibleRows.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
datasource[section].section = section
return datasource[section]
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if datasource[indexPath.section].visibleRows.count == 0 { return 0 }
return datasource[indexPath.section].visibleRows[indexPath.row].bounds.height
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if datasource[indexPath.section].visibleRows.count == 0 { return UITableViewCell() }
// I've left this stuff here to show the real contents of a cell - note how
// the phantom cell was returned before this point.
let section = datasource[indexPath.section]
let cell = TTSContentCell(withView: section.visibleRows[indexPath.row])
cell.accessibilityLabel = "cell_(indexPath.section)_(indexPath.row)"
cell.accessibilityIdentifier = "cell_(indexPath.section)_(indexPath.row)"
cell.showsReorderControl = true
return cell
}

最新更新