2011-11-02 35 views
1

我有NSFetchedResultsController,我需要在的tableView如何限制NSFetchResultsController中的對象數量?

顯示數據

我用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個數據?

回答

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 
    } 
    ... 
    ... 
    ... 
} 
}