如何知道从coredata填充的UITableView的最后一个单元格? [英] How to know the last cell of the UITableView which is populating from a coredata?

查看:98
本文介绍了如何知道从coredata填充的UITableView的最后一个单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个UITableview核心数据。我调用api和存储数据在我的本地db.Using fetchedResultsController 表正在显示。

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.

从服务器分页的数据每次来20-30项。如果我到达最后一个单元格,而滚动tableView我应该再次调用api获得下一个项目。

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.

这是我的 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;
}

我要查看这里

 -(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath {

    //code to check the last item in the core data from `fetchedResultsController`
}

因此如何检查该记录是否是db的最后记录?

So how to check if that record is last record of the db ?

推荐答案

countForFetchRequest:error :方法 NSManagedObjectContext 会告诉你你会看到多少项目。您还可以在获取的结果控制器上使用属性,查询(仅限?) NSFetchedResultsSectionInfo code> numberOfObjects

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.

一旦你有返回的对象数,你可以将它与 indexPath.row tableView:willDisplayCell:forRowAtIndexPath:中调用。

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.

这篇关于如何知道从coredata填充的UITableView的最后一个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆