删除部分的最后一行导致崩溃

Bar*_*tza 4 tableview ios swift

我已经四处冲浪Stackoverflow并发现了类似的问题,但我仍然无法弄清楚,因为我似乎做得对。

所以我有一个自定义单元格的表格视图。每当我尝试删除给定部分中的最后一行时,应用程序都会崩溃并显示以下错误消息:

更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(2),加上或减去插入或删除的节数(0插入,0删除)。

这是我的代码:

// shifts = [[Shift]] (Shift is my class)
func numberOfSections(in tableView: UITableView) -> Int {
    return shifts.count
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return shifts[section].count
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        // Removing the element from my list
        shifts[indexPath.section].remove(at: indexPath.row)

        // Removing empty lists if there are any
        var indexes = [Int]()
        for i in 0..<shifts.count {
            if shifts[i].count == 0 {
                indexes.append(i)
            }
        }
        var count = 0
        for number in indexes {
            shifts.remove(at: number-count)
            count += 1
        }

        // Deleting the row in the tableView
        myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.bottom)

    }
Run Code Online (Sandbox Code Playgroud)

其他帖子在他们的numberOfSectionsnumberOfRowsInSections方法中存在问题,但我的方法返回列表的计数,并且由于我在删除行之前从列表中删除了元素,因此我无法找出问题所在。

编辑根据 Lion 的建议添加的代码仍然抛出错误。

if tableView.numberOfRows(inSection: indexPath.section) > 1 {
            myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.bottom)
        } else {
            let indexSet = NSMutableIndexSet()
            indexSet.add(indexPath.section - 1)
            myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.bottom)
            myTableView.deleteSections(indexSet as IndexSet, with: UITableViewRowAnimation.bottom)
        }
Run Code Online (Sandbox Code Playgroud)

编辑 2:解决方案上面的代码有问题,因此引发错误。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {

    // Removing the element from my list
    shifts[indexPath.section].remove(at: indexPath.row)

    // Removing empty lists if there are any
    var indexes = [Int]()
    for i in 0..<shifts.count {
        if shifts[i].count == 0 {
            indexes.append(i)
        }
    }
    var count = 0
    for number in indexes {
        shifts.remove(at: number-count)
        count += 1
    }

    // Deleting the row in the tableView
    if tableView.numberOfRows(inSection: indexPath.section) > 1 {
        myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.bottom)
    } else {
        let indexSet = NSMutableIndexSet()
        indexSet.add(indexPath.section) // This line was wrong in above code.
        myTableView.deleteSections(indexSet as IndexSet, with: UITableViewRowAnimation.bottom)
    }

}
Run Code Online (Sandbox Code Playgroud)

Lio*_*ion 5

一旦尝试reloading tableview而不是删除行,我的意思是myTableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.bottom) 在 commiteditingStyle 方法中!

更新 :

这是因为如果您的部分没有元素,那么您将从数据源中删除部分我的意思是数组!所以,那个时候你必须删除部分也喜欢

 myTableView.deleteSections(<#T##sections: IndexSet##IndexSet#>, with: <#T##UITableViewRowAnimation#>)
Run Code Online (Sandbox Code Playgroud)

当您删除行时,如果它不是最后一行,那么您可以使用,deleteRow但是如果您想删除最后一行,那么您必须删除部分并且此时不要删除行!

或者正如我之前提到的那样重新加载整个表!