как ограничить количество объектов в NSFetchResultsController?

#xcode #tableview #nsfetchedresultscontroller

#xcode — код #просмотр таблицы #nsfetchedresultscontroller

Вопрос:

У меня есть 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:%dnnewIndexPath:%@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];
}
 

и перед выполнением выборки я использую

  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
 

Эта функция вызывается НЕ для установки 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 
   }
   ...
   ...
   ...
 }
}