[英]attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update
There is strange thing with may tableView可能 tableView 有一些奇怪的事情
I have我有
In my delegate I have在我的代表中,我有
var editingIndexPath: IndexPath?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let editingIndexPath = editingIndexPath {
return datasource.count + 1
} else {
return datasource.count
}
}
In my didSelect I have在我的 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)
}
}
}
The problem that it crashes after tableView.reloadData() with error tableView.reloadData() 报错后崩溃的问题
'attempt to insert row 3 into section 0, but there are only 3 rows in section 0 after the update' '尝试将第 3 行插入第 0 节,但更新后第 0 节中只有 3 行'
I dont get it.我不明白。 TableView has exactly same number of rows it needs to perform insert. TableView 执行插入所需的行数完全相同。 I set property to nil and then reload table, by this action I reduce table's rows number to two.我将属性设置为 nil,然后重新加载表,通过此操作,我将表的行数减少到两行。 Then I again set editingIndexPath
to not nil value signaling delegate method that it should use count + 1. But it fails same way.然后我再次将editingIndexPath
设置为not nil value 信号委托方法,它应该使用count + 1。但它以同样的方式失败。
And what is also interesting that the same code but without reload before never fails还有什么有趣的,相同的代码但没有重新加载之前永远不会失败
editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
if let editingIndexPath = editingIndexPath {
tableView.insertRows(at: [editingIndexPath], with: .top)
}
What's happening here ?这里发生了什么事 ?
Table view has numberOfRowsInSection 3 for section 0(Row1, Row1, Row3).表视图的第 0 部分(Row1、Row1、Row3)的 numberOfRowsInSection 3。 On trying to insert Row4, which exceeds numberOfRowsInSection count, the above-mentioned error is thrown在尝试插入超过 numberOfRowsInSection 计数的 Row4 时,抛出上述错误
This may work better( Note: Clueless of logic intended)这可能会更好(注意:逻辑无能)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// better to check with datasource array elements count
if indexPath.row + 1 < tableView.numberOfRows(inSection: indexPath.section) {
self.editingIndexPath = IndexPath(row: indexPath.row + 1, section: 0)
tableView.insertRows(at: [self.editingIndexPath!], with: .top)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.