Question

I'm working on an app using Stanford's CoreDataTableViewController. All database saving and fetching was working correctly until I removed the CDTVC files and then added them again. Now it's not fetching anything. CellForRowAtIndexPath is not being called and although the fetch request returns the correct number of entries in the database (reflecting each new entry), the numberOfRowsInSection and numberOfSections both return 0.

I have no idea what could have happened. I tried recreating the subclass, creating a completely new Table View Controller, I deleted the app from the simulator and from my device. My outlets as dataSource and delegate are set, the fetchedResultsController gets set and is not nil and the methods inside CoreDataTableViewController, setFetchedResultsController and performFetch, are still being called.

Thanks

Was it helpful?

Solution

final answer

Updating my answer after chat session. Switching to TLIndexPathTools's TLIndexPathController instead of NSFetchedResultsController resolved the issue.

answer #2

It sounds like you're saying the following method is returning zero:

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

And that you've verified the fetchedResultsController is set and that you've called performFetch and that the results are non-zero, i.e. the fetchedObjects property is returning a non-empty array. I don't see how all of those things can be true and have the above method return 0. Can you put a breakpoint in the above method and re-verify these things? Perhaps this method is getting called before you call performFetch.

If you continue to be stuck, you could try my TLIndexPathTools library. It provides an alternative to NSFetchedResultsController. Check out the Core Data sample project.

answer #1

Check that you're setting the fetchedResultsController property of your table view controller. From the description of your problem, it sounds like this property is nil.

OTHER TIPS

If tableView:cellForRowAtIndexPath: is not being called there is three possible solutions :

  • the tableview dataSource property is not set up properly ;
  • or numberOfSectionsInTableView: is returning 0 ;
  • or finally tableView:numberOfRowsInSection: is returning 0.

My guess, I would go for the third one from what you've told us.

And usually, with a fetched results controller you would use something close to that :

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows = 0;
    id sectionInfo = [_fetchedResultsController.sections objectAtIndex:section];
    rows = [sectionInfo numberOfObjects];
    return rows;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top