I have a table that gets data from core data. I added a section, in which there must be only one static cell. But I get an error and I can’t figure out why.
* Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]’
questo è il codice
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setupFetchedResultsController];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [[self.fetchedResultsController fetchedObjects] count];
} else if (section == 1) {
return 1;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Category Cell";
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
if (indexPath.section == 0) {
NSLog(@"section %i",indexPath.section);
Category *category = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.categoryNameLabel.text = category.name;
cell.categoryNumberItemsLabel.text = [NSString stringWithFormat:@"%i",category.containItem.count];
} else {
//Static cell
}
return cell;
}
Your two tableView datasource methods are not consistent.
In
numberOfRowsInSection, you return the total count of the fetched results controller’sfetchedObjects.In
cellForRow..., you are using the fetched results controller’sobjectAtIndexPathmethod.You should be obtaining the section info object and getting the row count from there, as per the documentation: