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!