delete section at indexPath swift

user2363025 picture user2363025 · Jan 4, 2016 · Viewed 14.5k times · Source

I have an array which populates a table view - myPosts.

The first row of the table view is not part of the array.

Each row is its own section (with its own custom footer)

I am trying to perform a delete with the following code:

func tableView(profileTableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
            if (editingStyle == UITableViewCellEditingStyle.Delete) {
                myPosts?.removeAtIndex(indexPath.section - 1)
                profileTableView.beginUpdates()
                let indexSet = NSMutableIndexSet()
                indexSet.addIndex(indexPath.section - 1)
                profileTableView.deleteSections(indexSet, withRowAnimation: UITableViewRowAnimation.Automatic)
                profileTableView.deleteRowsAtIndexPaths([indexPath],  withRowAnimation: UITableViewRowAnimation.Automatic)
                profileTableView.endUpdates()
                ...
                WS Call 
                ...
            }
}

And the log is reporting the following:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Obviously the issue is related to 0 moved in, 0 moved out but I don't understand why that is? or what the solution would be?

Number of sections in tableView is as follows:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        if self.myPosts == nil
        {

            return 1
        }
        return self.myPosts!.count + 1
    }

Answer

CodeBender picture CodeBender · Jul 20, 2017

Updated the answer for Swift 4.2 and made a few additional tweaks:

func tableView(_ tableView: UITableView,
               commit editingStyle: UITableViewCell.EditingStyle,
               forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        myPosts?.removeAtIndex(indexPath.section - 1)
        let indexSet = IndexSet(arrayLiteral: indexPath.section)
        profileTableView.deleteSections(indexSet, with: .automatic)
        // Perform any follow up actions here
    }
}

The use of beginUpdates() and endUpdates() is not necessary, since you are only doing one action that contains animation. If you are doing 2 or more, than it is worth combining them to get a fluid effect.

Also, this makes use of the Swift 3 classes, by doing away with the NSMutableIndexSet() call, which would require a conversion now to work with the deleteSections() call.