Reputation: 477
I am trying to set the numberOfRowsInSection: of my table view controller to the amount of items in a specific core data entity's attribute. For example, I have a string saved in core data in an attribute for every cell and I would like the same amount cell's as strings. I then have another attribute in the entity that contains an image for every cell. Because of the two attributes, I would like to set the amount of cell's to the amount of stings, not to the amount of objects in the entity. If I did it based on the amount of objects in the entity, I would have twice the amount of cells that I need. I have a way that I am trying to do this, but the table isn't creating any cells. My problem is no cells are currently being created with this numberOfRowsInSection:
How would I either fix this code so that the cells appear?
Here is how I am trying to do it:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FeedEntity" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:@[@"urlString"]];
request.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
// Execute the fetch.
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil) {
// Handle the error.
}
id sectionInfo =
[[theFetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
Upvotes: 0
Views: 63
Reputation: 477
My problem was that instead of saving both objects to core data at the same time, I had two methods to save them, one for the string and one for the image. I just condensed them into one method and it worked perfectly.
Upvotes: 1
Reputation: 4953
So long as all of your attribute names are correct, I don't inherently see anything wrong with your numberOfRowsInSection:
implementation. Where you check objects
for nil
, you should also check error
for != nil
to see if CoreData created an error. Since you're not getting what you expect from this method, you might be getting an error.
You're fetching only the urlString
property but sorting by timeStamp
. Contrary to what I wrote before, that is inherently wrong. With selectDistinctResults
, you can't sort by a property you don't fetch. That could be your problem.
Other thoughts might be that numberOfObjects
is actually zero, due to you not saving any inserted data in CoreData or sectionInfo
being nil
. You might not have the dataSource
property of your UITableView
wired in correctly and so this method isn't getting called.
You might also consider externalizing your NSFetchedResultsController
and using NSFetchedResultsControllerDelegate
to be called from numberOfRowsInSection:
. That way you immediately get updates when new entries are added or deleted.
Upvotes: 0