如何知道从核心数据填充的 UITableView 的最后一个单元格?
How to know the last cell of the UITableView which is populating from a coredata?
我正在使用核心 data.I 的 UITableview 正在调用 api 并将数据存储在我的本地 db.Using fetchedResultsController
table 正在显示。
来自服务器的分页数据每次 20-30 items.So 如果我在滚动 table 视图时到达最后一个单元格,我应该再次调用 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`
}
那么如何检查该记录是否是数据库的最后一条记录?
NSManagedObjectContext
上的 countForFetchRequest:error:
方法会告诉您将要看到多少项目。您还可以在获取的结果控制器上使用 sections
属性,查询(仅?)NSFetchedResultsSectionInfo
实例的 numberOfObjects
.
获得要返回的对象数量后,您可以将其与 tableView:willDisplayCell:forRowAtIndexPath:
调用中的 indexPath.row
进行比较。
我正在使用核心 data.I 的 UITableview 正在调用 api 并将数据存储在我的本地 db.Using fetchedResultsController
table 正在显示。
来自服务器的分页数据每次 20-30 items.So 如果我在滚动 table 视图时到达最后一个单元格,我应该再次调用 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`
}
那么如何检查该记录是否是数据库的最后一条记录?
NSManagedObjectContext
上的 countForFetchRequest:error:
方法会告诉您将要看到多少项目。您还可以在获取的结果控制器上使用 sections
属性,查询(仅?)NSFetchedResultsSectionInfo
实例的 numberOfObjects
.
获得要返回的对象数量后,您可以将其与 tableView:willDisplayCell:forRowAtIndexPath:
调用中的 indexPath.row
进行比较。