问题
I am using a UITableview with core data.I am calling api and storing data in my local db.Using fetchedResultsController table is getting displayed.
From server paginated data is coming each time 20-30 items.So if I reached last cell while scrolling the tableView I should call the api again to get the next items.
This is my fetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController {
if (! _fetchedResultsController) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Data"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"saved_at" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:10];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
}
return _fetchedResultsController;
}
I have to check here
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
//code to check the last item in the core data from `fetchedResultsController`
}
So how to check if that record is last record of the db ?
回答1:
The countForFetchRequest:error: method on NSManagedObjectContext will tell you how many items you're going to see. You can also use the sections property on your fetched results controller, querying the (only?) NSFetchedResultsSectionInfo instance for its numberOfObjects.
Once you have the number of objects that will be returned, you can compare that to the indexPath.row in your tableView:willDisplayCell:forRowAtIndexPath: call.
来源:https://stackoverflow.com/questions/34085702/how-to-know-the-last-cell-of-the-uitableview-which-is-populating-from-a-coredata