我有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];
}
在performFetch之前我使用
request.fetchLimit = 100;
request.fetchBatchSize = 100;
这意味着我想显示数据只有100,但是当我做一些会使NSManagedObjectContext被更改的东西时,它会调用NSFetchedResultsControllerDelegate并将任何数据插入到tableView中。问题是,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个数据?
答案 0 :(得分:1)
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
此函数不是调用setOfRowsInSection,而是调用数字。
-(void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type {
DLog(@"didChangeSection: %@", controller);
switch(type) {
case NSFetchedResultsChangeInsert:
if([YourtableView numberOfRowsInSection:yourSection]>100){
return; //TO avoid insertion
}
...
...
...
}
}