【发布时间】:2018-04-12 15:28:53
【问题描述】:
may tableView 有点奇怪
我有
在我的委托中,我有
var editingIndexPath: IndexPath?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let editingIndexPath = editingIndexPath {
return datasource.count + 1
} else {
return datasource.count
}
}
在我的 didSelect 我有
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let oldEditingIndexPath = editingIndexPath {
self.editingIndexPath = nil
tableView.reloadData()
self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
tableView.insertRows(at: [self.editingIndexPath!], with: .top)
} else {
editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
if let editingIndexPath = editingIndexPath {
tableView.insertRows(at: [editingIndexPath], with: .top)
}
}
}
tableView.reloadData() 报错后崩溃的问题
'尝试将第3行插入第0节,但更新后第0节只有3行'
我不明白。 TableView 具有执行插入所需的完全相同的行数。我将属性设置为 nil,然后重新加载表,通过这个操作我将表的行数减少到两个。然后我再次将editingIndexPath 设置为非零值信号委托方法,它应该使用count + 1。但它以同样的方式失败。
同样有趣的是,相同的代码但没有重新加载之前永远不会失败
editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
if let editingIndexPath = editingIndexPath {
tableView.insertRows(at: [editingIndexPath], with: .top)
}
这里发生了什么?
【问题讨论】:
-
您为什么将
tableView.reloadData() right after setting theeditingIndexPath` 称为nil?您试图采取的行为有点不清楚。您是否试图始终为editingIndexPath提供价值? -
主要是为了调试,我有语句在 tableview 重新加载后打印行数,它打印了 2
标签: ios uitableview