【问题标题】:how to limit number of objects in NSFetchResultsController?如何限制 NSFetchedResultsController 中的对象数量?
【发布时间】:2011-11-02 08:35:54
【问题描述】:

我有 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条数据?

【问题讨论】:

    标签: xcode tableview nsfetchedresultscontroller


    【解决方案1】:
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    

    此函数不是调用设置 numberOfRowsInSection 而是获取数字。

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

    【讨论】:

      猜你喜欢
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多