问题描述
我有一个由 2 个部分组成的 UITableView.行最初打印在第 0 部分.它们必须可在部分之间移动.
I have a UITableView consisting of 2 sections. Rows are initially printed in section 0. They must be moveable between sections.
当我将单元格从第 0 部分移动到第 1 部分时,我得到:
When I move a cell from section 0 to section 1, I get:
"Attempt to move index path to index path that does not exist"
这是我的代码.
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
self.tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)
}
这些都返回正确的值:
print("source row \(sourceIndexPath.row)")
print("source section \(sourceIndexPath.section)")
print("destination row \(destinationIndexPath.row)")
print("destination section \(destinationIndexPath.section)")
关于如何解决这个问题的任何想法?
Any ideas on how I can solve this?
更新:
我已经查看了 fetchedResultsControllerDelegate,现在我有了这个:
I've looked into the fetchedResultsControllerDelegate and now I have this:
override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let movedObject = self.fetchedResultsController.objectAtIndexPath(sourceIndexPath) as! NSManagedObject
print(movedObject)
controller(self.fetchedResultsController, didChangeObject: movedObject, atIndexPath: sourceIndexPath, forChangeType: NSFetchedResultsChangeType.Delete, newIndexPath: destinationIndexPath)
self.tableView.deleteRowsAtIndexPaths([sourceIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
controller(self.fetchedResultsController, didChangeObject: movedObject, atIndexPath: sourceIndexPath, forChangeType: NSFetchedResultsChangeType.Insert, newIndexPath: destinationIndexPath)
self.tableView.insertRowsAtIndexPaths([destinationIndexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
现在错误是无效更新:
无效更新:第 0 节中的行数无效.更新后现有节中包含的行数 (2) 必须等于更新前该节中包含的行数 (2),加上或减去从该部分插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该部分的行数(0 移入,0 移出).'
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
我先删除然后插入,所以从技术上讲,行数应该仍然相同.
I'm doing a delete then insert, so technically the number of rows should still be the same.
推荐答案
Update model in your datasource
是将数据填充到表格的数组然后使用 moveRowAtIndexPath
Update model in your datasource
is your array that fill data to table then use moveRowAtIndexPath
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedObject = self.datasource[sourceIndexPath.row]
datasource.remove(at: sourceIndexPath.row)
datasource.insert(movedObject, at: destinationIndexPath.row)
self.tableView.moveRowAtIndexPath(sourceIndexPath, toIndexPath: destinationIndexPath)
}
不要忘记启用移动
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
这篇关于(Swift) UITableView - 尝试将索引路径移动到不存在的索引路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!