Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 980
insomiac Avatar asked Jul 29 '12 02:07

insomiac


3 Answers

I ran into this error when I forgot to set the datasource outlet properly.

If you see this error, check that you have explicitly set your TableView delegate and datasource :

  • go to your interface builder and look at the view with the 'Table View'

  • cmd + right click drag ( you should see a blue line ) from the 'Table View' icon to the title icon for the file, this has a yellow icon next to it in xcode 6.

  • release your mouse , you should see delegate and datasource options.

  • select 'datasource'

your table should now be correctly wired.

like image 101
NiallJG Avatar answered Nov 10 '22 15:11

NiallJG


I've ran into this problem before. It means that when -insertRowsAtIndexPaths:withRowAnimation: was called -tableView:numberOfRowsInSection: returned 0. You need to insert into the model before you call -insertRowsAtIndexPaths:withRowAnimation: (see: -beginUpdates)


Update

I wonder what the return value of -tableView:numberOfRowsInSection: is? Also, you don't need -beginUpdates/-endUpdates if you only have one update.

[timeZoneNames insertObject:@"HELLO" atIndex:0];
// Let's see what the tableView claims is the the number of rows.
NSLog(@"numberOfRowsInSection: %d", [self tableView:self.tableView numberOfRowsInSection:0]);
NSArray *insertIndexPaths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationTop];
like image 28
Jeffery Thomas Avatar answered Nov 10 '22 14:11

Jeffery Thomas


I tried printing out how many rows and sections were actually in the tableView during the update which got me thinking, how many sections am I returning in the table view data source method...and this was the culprit:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 0;
}

Make sure you are returning 1 and not 0.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

At least this solved the problem for me...

UPDATE:

I'm having the same problem again after it was working for a while. I had modified the app a bit. My first view is a tableView and when you click the plus button on the upper right, you get another table where you can input information. When you click done, the information is added to the Core Data model with this code:

- (IBAction)doneButtonPressed:(UIBarButtonItem *)sender
{

MoneyBadgeAppDelegate *appDelegate =
[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context =
[appDelegate managedObjectContext];
NSManagedObject *newMoment;
newMoment = [NSEntityDescription
             insertNewObjectForEntityForName:@"SpendingMoment"
             inManagedObjectContext:context];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *modifiedDate = [dateFormat dateFromString: self.dateTextField.text];

[newMoment setValue: modifiedDate forKey:@"date"];
[newMoment setValue: descTextField.text forKey:@"desc"];

[appDelegate.eventsArray insertObject:newMoment atIndex:0];

NSError *error;
[context save:&error];

[self dismissViewControllerAnimated:YES completion:nil];

}

And I confirmed it gets put into the Core Data model successfully by printing to the console the following in my viewDidAppear method upon returning back to the first screen.

-(void)viewDidAppear:(BOOL)animated{
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SpendingMoment"  
inManagedObjectContext:self.managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];

NSError *error;
NSArray *items = [self.managedObjectContext
                  executeFetchRequest:fetchRequest error:&error];

for (SpendingMoment *moment in items) {
    NSLog(@"Date: %@, Desc: %@", [moment date], [moment desc]);
}

[self addEvent];

And then my addEvent method does this:

- (void)addEvent {

[self.tableScreen beginUpdates];

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableScreen insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                      withRowAnimation:UITableViewRowAnimationFade];


[self.tableScreen reloadData];
[self.tableScreen endUpdates];

@try{
    [self.tableScreen scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
@catch(NSException *exception){
}

}

Any idea what the problem is this time??? Thanks!

like image 4
Angie Avatar answered Nov 10 '22 13:11

Angie