I’ve got a .plist file with NSMutableDictionary that I am populating a UITableView like so:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myFile = [[NSBundle mainBundle]
pathForResource:@"courses" ofType:@"plist"];
courses = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];
courseKeys = [courses allKeys];
}
And:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Course";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSString *currentCourseName = [courseKeys objectAtIndex:[indexPath row]];
[[cell textLabel] setText:currentCourseName];
return cell;
}
I am trying to allow the user to swipe + delete and also “add” new items to the plist using the UiTableView cells. This is the code I’m using to do this:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
When I run the simulator and do this it gives me an error, stating
“Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), 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).'”
Can anyone help me out here?
You are removing the row from the table but not from your data source. CourseKeys needs to be a mutableArray that you remove deleted keys from.