My goal is populating a section of a tableview by filling data from all fetched results on a fetchedResultsController. This is very straighforward and works like a charm.
The second part of my small app is adding a attribute “price” in another section of the same table. It works allright.
This is I’ve used to achieve that:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 1) {
return 1;
} else return [self.fetchedResultsController.fetchedObjects count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
static NSString *ci = @"CellTotal";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ci];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:0];
float expense = 0;
for (NSManagedObject *object in [sectionInfo objects]) {
expense += [[object valueForKey:@"precio"] floatValue];
}
cell.textLabel.text = [NSString stringWithFormat:@"%f", expense];
return cell;
} else {
static NSString *ci = @"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:ci];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:indexPath];
cell.detailTextLabel.text = g.nombre;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
[f setMinimumFractionDigits:2];
[f setMaximumFractionDigits:2];
NSString *precio = [f stringFromNumber: g.precio];
cell.textLabel.text = precio;
}
The problem comes up when I wanted to change the order of the sections in the tableview. If I wanted to show the total addition in section 0, and the list of fetchedresults in section 1, then my app crashes with the error:
Terminating app due to uncaught exception ‘NSRangeException’, reason:
‘* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]’
Any ideas on what I’m doing wrong? any help is much appreciated, thanks!
update
This is where the code breaks the app:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
Gastos *g = (Gastos *)[self.fetchedResultsController objectAtIndexPath:indexPath];
It look like be working on the first fetched object but fails on the second. I’m really struggling with this question. I’ve been into debugging, checking whether fetch controller exists, etc, and everything seems to be allright and it breaks…
I’m a noob and do not know how to fix the issue 🙁
You have a table view with 2 sections. The second section is populated by a FRC (which has only one section).
Therefore section 1 in the table view corresponds to section 0 in the FRC.
Your
configureCell:atIndexPath:method is called with the table view’s indexPath (section == 1), butobjectAtIndexPathmust be called with section == 0. So you must adjust the section number like this: