如何限制 NSFetchResultsController 中的对象数量?
我有 NSFetchedResultsController ,我需要在 tableView 上显示数据
我使用 NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
DLog(@"controllerWillChangeContent: %@", controller);
[self.tableViewA beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
DLog(@"didChangeSection: %@", controller);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableViewA insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableViewA deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
DLog(@"controller:%@\ndidChangeObject:%@\natIndexPath:%@\nforChangeType:%d\nnewIndexPath:%@\n",controller, anObject, indexPath, type, newIndexPath);
UITableView *tableView = self.tableViewA;
DLog(@"%@", self.tableViewA);
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeUpdate:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];
//[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableViewA endUpdates];
}
,在执行 Fetch 之前我使用
request.fetchLimit = 100;
request.fetchBatchSize = 100;
这意味着我只想显示 100 个数据,但是当我做一些使 NSManagedObjectContext 发生更改的事情时,它将调用 NSFetchedResultsControllerDelegate 并插入任何数据到表视图。问题是,tableView可以显示超过100条数据。
我使用这个函数设置numberOfRowsInSection,
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.FetchController sections] objectAtIndex:section];
CalledRowCountingNotYetCallRowForSection=true;
[self.tableViewA setBounces:YES];
if([sectionInfo numberOfObjects]>100){
//[Timer searchCriteriaChanged];
CLog(@"Something Wrong This NumberOfRow: %d", [sectionInfo numberOfObjects]);
}
else{
}
return [sectionInfo numberOfObjects];
}
如何使tableView不能显示超过100条数据?
I have NSFetchedResultsController that I need to show data at tableView
I use NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
DLog(@"controllerWillChangeContent: %@", controller);
[self.tableViewA beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
DLog(@"didChangeSection: %@", controller);
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableViewA insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableViewA deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
DLog(@"controller:%@\ndidChangeObject:%@\natIndexPath:%@\nforChangeType:%d\nnewIndexPath:%@\n",controller, anObject, indexPath, type, newIndexPath);
UITableView *tableView = self.tableViewA;
DLog(@"%@", self.tableViewA);
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeUpdate:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationNone];
//[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableViewA endUpdates];
}
and before performFetch I use
request.fetchLimit = 100;
request.fetchBatchSize = 100;
that's mean I want to show data just 100, but when I do something that will make NSManagedObjectContext was changed, it will call NSFetchedResultsControllerDelegate and will insert any data to tableView. the problem is, the tableView can show more than 100 data..
I use this function to set numberOfRowsInSection
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.FetchController sections] objectAtIndex:section];
CalledRowCountingNotYetCallRowForSection=true;
[self.tableViewA setBounces:YES];
if([sectionInfo numberOfObjects]>100){
//[Timer searchCriteriaChanged];
CLog(@"Something Wrong This NumberOfRow: %d", [sectionInfo numberOfObjects]);
}
else{
}
return [sectionInfo numberOfObjects];
}
how can I make tableView can't show more than 100 data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用此函数不是为了设置 numberOfRowsInSection,而是为了获取数字。
This function is NOT call to set numberOfRowsInSection but to GET the number.