How to know the last cell of the UITableView which is populating from a coredata?

这一生的挚爱 提交于 2019-12-12 04:15:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!