4

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
    }
CodeBender
  • 35,668
  • 12
  • 125
  • 132
user2363025
  • 6,365
  • 19
  • 48
  • 89
  • bear in mind, after you have deleted the section at index 0, the section at index 1 will become the new section at index 0; therefore you need to be aware of reflecting that in your _data-source_ as well (which we don't know anything about), and you need to do something with the rows as well. – holex Jan 04 '16 at 15:53
  • @holex my data source is updated in the Webservice call that's commented out there. What else do i need to do with the rows apart from deleteRowsAtIndexPath which is in there already? – user2363025 Jan 04 '16 at 15:55
  • you __must finish__ updating your local _data-source_ before you call the `–endUpdates` method; otherwise you are doomed. – holex Jan 04 '16 at 15:56
  • @holex and the line: myPosts?.removeAtIndex(indexPath.section - 1) does not handle updating the data source?! – user2363025 Jan 04 '16 at 16:01
  • on the other hand, logically if the section's number _are not changing_ after "delete" (because you will show 1 section anyway after the last one is "deleted"), you must not call the `-deleteSections:withRowAnimation:` method, you just need to call the `-reloadSections:withRowAnimation:` method only; because __the section's number will be the same__ after "deleting" it; you just want to get it updated. – holex Jan 04 '16 at 16:03
  • @holex switching out deleteSections to reloadSections is not making a difference. Same error appearing in the log. Also I'm not sure what you mean by section's number. The section's index or the section count? – user2363025 Jan 04 '16 at 16:06
  • please, read some tutorials about that first, it is not a simple _switching from this method to that_, it is much bigger logically than it could be explained in a short comment if you have not got the basic concept of working with `UITableView`, I'm afraid. :( I could highlight where you need to start finding the solution only at, but it is not suitable for tutorials. – holex Jan 04 '16 at 16:09
  • 2
    I think the problem is calling `deleteRowsAtIndexPaths` after `deleteSections` wasn't calling `deleteSections` enough? – Ismail Jan 04 '16 at 16:15
  • @holex thanks for your help but everything you've said thus far apart from reloading instead of deleting, I thought I had covered. i.e. you said I need to update the datasource, I pointed to the line where I believed to be doing so. You said I also need to do 'something' with the rows. i also asked if the line 'deleteRowsAtIndexPath' was the handling you were referring to? I have never worked with 'reloading' sections as opposed to deletingSections. But if we are to stick with deleting, I still don't understand the issue. And I'm still not sure if by section number you meant index or count – user2363025 Jan 04 '16 at 16:16
  • @Ismail thank you, that was it! :) – user2363025 Jan 04 '16 at 16:18
  • @user2363025, that is the exact reason why I recommended to walk through some tutorials; but if you have found the solution in changing the lines' order randomly, that is still good for now. – holex Jan 04 '16 at 16:20

2 Answers2

13

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.

CodeBender
  • 35,668
  • 12
  • 125
  • 132
7

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 
            ...
        }
   }
Chris Emerson
  • 13,041
  • 3
  • 44
  • 66
Ismail
  • 2,778
  • 2
  • 24
  • 39