NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'

Viewed 57930

I am using UITableViewController and getting this error while updating tableView. Below is my code:

This occurs when i do a click event:

[timeZoneNames insertObject:@"HELLO" atIndex:0];  
[self.tableView beginUpdates];   
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];

I tried looking for apple documentation but that didnt helped.

Thanks, Aby

4 Answers

Check this "attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update".

It mesns when you insert object in row 0 , section 0, there are no any sections. Try to insert section before you insert row.

 [timeZoneNames insertObject:@"HELLO" atIndex:0];
 NSLog(@"sections: %lu ",[self.downlaodTableView numberOfSections];
 // insert section 
 [self.tableView insertSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 1)] withRowAnimation:UITableViewRowAnimationNone];
 NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView  numberOfRowsInSection:0]);
 NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
 [self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
Related