UITableView Crashing When Editing
Getting error at line: [self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
* 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 (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).
开发者_运维百科- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.routineTableView setEditing:editing animated:animated];
if(editing){
[self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
} else {
// delete section
}
}
Update:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
Please make sure that you return current number of sections after update. This means you'll need a way to track number of sections, example in a private variable currentNumberOfSections
that should be NSInteger
. Increment it after each insert (decrement in case you're removing a section). And don't forget to update your -numberOfSectionsInTableView
method in your TableViewController
to something like this:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return currentNumberOfSections;
}
Check that you are returning the current number of sections for the TableViewController
, in which you're adding the new section.
In your case it would be something like this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.routineTableView setEditing:editing animated:animated];
if(editing){
[self.routineTableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
currentNumberOfSections++;
} else {
// delete section
}
And then in the class referenced by self.routineTableView
add/update:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return currentNumberOfSections;
}
Don't forget to add NSInteger currentNumberOfSections
to controller's header file. I would also put it in @private
section, but this is up to you. In the designated initializer of the controller set the value of this variable to preferred default number of sections: currentNumberOfSections = 1
.
Good luck!
精彩评论