如何知道从coredata填充的UITableView的最后一个单元格?

时间:2015-12-04 10:05:29

标签: uitableview core-data nsfetchedresultscontroller

我正在使用带有核心数据的UITableview。我正在调用api并将数据存储在我的本地db中。正在显示使用fetchedResultsController表。

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

这是我的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的最后一条记录?

1 个答案:

答案 0 :(得分:1)

countForFetchRequest:error:上的NSManagedObjectContext方法会告诉您将要查看的项目数量。您还可以在获取的结果控制器上使用sections属性,查询NSFetchedResultsSectionInfo的{​​only?)numberOfObjects实例。

获得返回的对象数量后,您可以将其与indexPath.row来电中的tableView:willDisplayCell:forRowAtIndexPath:进行比较。