【发布时间】:2020-02-19 04:11:35
【问题描述】:
我正在尝试使用滑动操作删除 UITableView 中的一行,但一直收到以下错误:
'NSInternalInconsistencyException',原因:'试图从第 1 节中删除第 2 行,它只包含更新前的 2 行'
tableview 有多个部分。
代码如下:
override func numberOfSections(in tableView: UITableView) -> Int {
return favouriteSubjectTitlesArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return favouriteCaseIDsBySubjectArray[section].count
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
if orientation == .left {
//Unfavourite the case
let unfavouriteAction = SwipeAction(style: .destructive, title: "Unfavourite") { action, indexPath in
self.favouriteCaseIDsBySubjectArray[self.selectedSubjectID].remove(at: indexPath.row)
DispatchQueue.main.async {
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
return [unfavouriteAction]
}
return nil
}
任何想法我哪里出错了?
【问题讨论】:
-
存在不一致:在
numberOfSections中有favouriteSubjectTitlesArray,在numberOfRowsInSection中有favouriteCaseIDsBySubjectArray代表这些部分。数据源不喜欢不一致。你应该从indexPath.section删除 -
好的,将尝试简化它。从
indexPath.section删除是什么意思? -
您应该使用
self.favouriteCaseIDsBySubjectArray[indexPath.section].remove(at: indexPath.row)来确保您从正确的数组中删除
标签: ios swift uitableview