本文介绍了删除部分中的最后一个单元格后,iOS 应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以每次我尝试删除该部分的最后一个单元格时,我的应用都会崩溃.

So I've my app crashing everytime I try to delete the last cell of the section.

例如,如果我的部分有 10 行,我可以毫无问题地删除它们,但最后一个会引发以下错误:

Ex., if my section has 10 rows, I can delete them without any problem but the last one throws the following error:

由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:无效更新:节数无效.更新后表视图中包含的节数(1)必须等于更新前表视图中包含的节数(3),加上或减去插入或删除的节数(0插入,0已删除).'

我已经在这里搜索并找到了一些解决此问题的方法,但我尝试了所有方法并无法解决此问题,它仍然崩溃并抛出相同的错误.

I've searched around here and found some ways to fix this problem, but I tried them all and couldn't get this issue fixed, it still crashes and throws the same error.

我的代码的相关部分如下:

The related parts of my code goes as it follows:

override func numberOfSections(in tableView: UITableView) -> Int {
    if (main_arr.count > 0 && sub_arr.count > 0) {
        self.numberOfSections = 3
    } else {
        self.numberOfSections = 2
    }

    return self.numberOfSections
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if (section == 0) {
        return 1
    } else if (section == 1 && main_arr.count > 0) {
        return main_arr.count
    } else {
        return sub_arr.count
    }

}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        switch indexPath.section {

        case 1:
            main_arr.remove(at: indexPath.row)

        case 2:
            sub_arr.remove(at: indexPath.row)

        default:
            print("default 001")
        }

        tableView.deleteRows(at: [indexPath], with: .fade)

    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

编辑 1

我尝试处理负责 numberOfSections 的全局变量,因此当我的任何一个 arrays.count == 0 时,它将减 1,但它没有解决问题.

I tried to handle the global variable, responsible for the numberOfSections so when any of my arrays.count == 0 it will be decreased by 1, but it didn't solved the issue.

我确实完全理解错误消息,例如,如果我有 3 个部分,并且我删除了其中一个部分的全部内容,我应该删除一个部分并将其也从数据源中删除.

I do understand completely the error message, like, if I've 3 sections, and I delete the whole content of one of them, I should delete one section and delete it from the datasource as well.

推荐答案

问题是numberOfSections在你删除行之前和之后返回不同的值,但是你并没有删除任何部分.因此,除了 deleteRows

The problem is that numberOfSections returns different values before and after you delete rows, but you don't delete any sections. So you should either return a constant value in numberOfSections or call deleteSections in addition to deleteRows

要记住的主要内容如下:

The main thing to remember is the following:

UITableView 必须始终包含与数据源相同数量的行和部分.

您不能只在 numberOfSectionsnumberOfRows 数据源方法中返回一个新值.每次更改都应使用删除/插入行(节)方法进行补偿.反之亦然:如果删除/插入 tableView,则必须在 dataSource 方法中返回相应的值.正如您的异常消息所述:

You cannot just return a new value in numberOfSections or numberOfRows dataSource methods. Every change should be compensated with delete/insert rows(sections) method. And vice versa: if you delete/insert to tableView, you must return corresponding values in dataSource methods. Just as your exception message states:

表视图中包含的节数更新后(1) 必须等于表中包含的节数查看更新前(3),加减版块数插入或删除(插入 0 个,删除 0 个).

因为3+0≠1.在这种情况下,您应该删除两个部分以避免崩溃.

It's because 3+0≠1. In this case you should have deleted two sections to avoid crash.

这篇关于删除部分中的最后一个单元格后,iOS 应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:00