Ian Gray
Ian Gray

Reputation: 387

NSFetchedResultsController W/ Custom Table Cell Incompatible Pointer

I've been trying to get a grip on CoreData. After several attempts to find a good tutorial on it I made a lot of progress with a tutorial on youtube. However when I got to the NSFetchedResultsController I'm getting a warning on my custom table cell ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath] stating that it's an incompatible pointer initializing with UITableViewCell (the tutorial does not use custom cells).

Even worse, when I attempt to add an item I get this nice ugly error.

2013-12-04 19:15:48.407 Ping[490:60b] * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1138 2013-12-04 19:15:48.409 Ping[490:60b] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update with userInfo (null)

I've spent hours trying to figure out what I'm doing wrong but I can't wrap my head around it. So if anyone can take a gander at my code and give me suggestions on how I can fix it I'd greatly appreciate it. Thank you.

Controller:

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.ContactsListTableView;
    switch (type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate: {
            Contact *changeContact = [self.fetchedResultsController objectAtIndexPath:indexPath];
            ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.name.text = [NSString stringWithFormat:@"%@ %@", changeContact.contactFirstName, changeContact.contactLastName];
        }
            break;
        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

EDIT: My implementation of number of rows and sections.

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

fetchedResultsController is defined in the interface simply as @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController

Upvotes: 1

Views: 1040

Answers (1)

Chris
Chris

Reputation: 1673

The warning is referring to the fact that the method

[tableView cellForRowAtIndexPath:indexPath];

returns a UITableViewCell, which is not literally your ITDContactCell. Placing a cast before the method should remove the warning.

ITDContactCell *cell = (ITDContactCell*) [tableView cellForRowAtIndexPath:indexPath];

The other error is probably because of some lack of relationship between your fetchedResultsController and your tableView. Showing your tableView delegate and source methods would help. I assume you have already implemented something similar:

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSError *error = nil;
    if(![self.fetchedResultsController performFetch:&error]) {
        // NSLog(@"Error! %@", error);
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _fetchedResultsController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [secInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    ITDContactCell *cell = (ITDContactCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // assign cell content

    return cell;
}

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

-(NSFetchedResultsController *) fetchedResultsController {
    if(_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                                    managedObjectContext:self.managedObjectContext
                                                                      sectionNameKeyPath:nil
                                                                               cacheName:nil];
    _fetchedResultsController.delegate = self;
    return  _fetchedResultsController;
}

-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.ContactsListTableView;
    switch (type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate: {
            Contact *changeContact = [self.fetchedResultsController objectAtIndexPath:indexPath];
            ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.name.text = [NSString stringWithFormat:@"%@ %@", changeContact.contactFirstName, changeContact.contactLastName];
        }
            break;
        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

EDIT 1:

As a note, you should not be using the reloadData method in your tableview, since the table is reloaded (via the fetched results controller) after saving changes to your NSManagedObjectContext.

EDIT 2:

Is you NSManagedObjectContext nil?

Upvotes: 1

Related Questions