Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete section at indexPath swift

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
    }
like image 858
user2363025 Avatar asked Nov 30 '22 10:11

user2363025


2 Answers

So the answer is just removing the line that deletes the rows.

So the code is here to delete:

 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 
            ...
        }
   }
like image 32
Ismail Avatar answered Dec 05 '22 14:12

Ismail


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.

like image 119
CodeBender Avatar answered Dec 05 '22 14:12

CodeBender