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

时间:2017-11-11 12:35:51

标签: ios swift tableview

我已经浏览过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)

    }

其他帖子在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)
        }

编辑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)
    }

}

1 个答案:

答案 0 :(得分:3)

一旦尝试 myTableView.deleteSections(<#T##sections: IndexSet##IndexSet#>, with: <#T##UITableViewRowAnimation#>) 而不是删除行,我的意思是提交editStyle方法中的deleteRow

更新:

这是因为如果您的部分没有元素,那么您正在从数据源中删除部分我的意思是数组!那么,那时你必须删除部分,如

{{1}}

当您删除行时,如果它不是最后一个,那么您可以使用{{1}}但如果要删除最后一行,则必须删除该部分,此时不要删除行!

或者像我之前提到的那样重新整理整个表格!

相关问题