简体   繁体   中英

Crashing when removing a row in a grouped table in Swift

I'm following a tutorial on learning to code using Swift. In it I'm asked to create an array of arrays which is used to create a grouped table. eg var emojis = [[emojiarray1],[emojiarray2],[emojiarray3]]

A later exercise asks me to incorporate code so that a swipe will remove a row from the table view. Here is what I have inside the function that helps carry out this process:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            emojis.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
}

However, when I swipe to remove a row, instead of the row deleting I get an app crash with the message: Thread 1: Signal SIGABRT

And in the console I get

 *** Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Sections:]

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (7) must be equal to the number of sections contained in the table view before the update (8), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

It says 'invalid number of sections', but I haven't removed a section and only a row in a section. I am thinking that maybe the emojis.remove(at: indexPath.row) line may have removed a section, but I don't really know if thats true, or how to go about fixing it.

You delete a row from the table but remove a section from the array, you may need

emojis[indexPath.section].remove(at: indexPath.row)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM