Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sectioning Fetched Results Controller breaks Search Display

I'm absolutely bamboozled by this and can't work out what am I missing. Perhaps a lot still to learn about core data.

This is my Fetched Results Controller which displays a section-less table but my search display controller works:

self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                    managedObjectContext:self.buckyballDatabase.managedObjectContext
                                                                      sectionNameKeyPath:nil
                                                                               cacheName:nil];

WHEREAS as soon as I apply a sectionNameKeyPath, to add sections. Sections get added but my search display controller is broken, throwing the below error:

*** Assertion failure in -[UITableViewRowData rectForRow:inSection:], /SourceCache/UIKit_Sim/UIKit-2372/UITableViewRowData.m:1630
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect at invalid index path (<NSIndexPath 0x7481040> 2 indexes [0, 1])'

And here are my Table view data source implementations Where filteredList is an NSArray:-

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView)
    {
        return [[self.fetchedResultsController sections] count];
    }
    else
    {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    if (tableView == self.tableView)
    {
        return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
    }
    else
    {
        return [self.filteredList count];
    }
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView == self.tableView)
    {
        return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
    }
    else
    {
        return nil;
    }
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if (tableView == self.tableView)
    {
        if (index > 0)
        {
            return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index-1];
        }
        else
        {
            self.tableView.contentOffset = CGPointZero;
            return NSNotFound;
        }
    }
    else
    {
        return 0;
    }
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (tableView == self.tableView)
    {
        NSMutableArray *index = [NSMutableArray arrayWithObject:UITableViewIndexSearch];
        NSArray *initials = [self.fetchedResultsController sectionIndexTitles];
        [index addObjectsFromArray:initials];
        return index;
    }
    else
    {
        return nil;
    }
}

Exception is thrown in cellForRowAtIndexPath at the below line:-

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                             forIndexPath:indexPath];

Please do ask if need to provide more information.. I'm scratching my head so much on this one..

like image 267
Bilal Wahla Avatar asked Nov 17 '12 04:11

Bilal Wahla


1 Answers

Try using -dequeueReusableCellWithIdentifier: and not -dequeueReusableCellWithIdentifier:forIndexPath:.

like image 115
chris Avatar answered Oct 17 '22 06:10

chris