iPhone — breaking up Core Data into sections with NSFetchResultsController

后端 未结 1 1880
后悔当初
后悔当初 2021-02-04 18:32

So I have successfully implemented Core Data to retrieve objects from a server, save them, and display them in a UITableView. However now, I wish to break these up into separate

相关标签:
1条回答
  • 2021-02-04 19:01

    The documentation for NSFetchedResultsController has sample code that works perfectly.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return [[self.fetchedResultsController sections] count];
    }
    
    - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo numberOfObjects];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        UITableViewCell *cell = /* get the cell */;
        NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
        // Configure the cell with data from the managed object.
        return cell;
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [sectionInfo name];
    }
    
    - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
        return [self.fetchedResultsController sectionIndexTitles];
    }
    
    - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
        return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
    }
    

    Set the sortDescriptors of the fetch request so the results are sorted by articleSection.
    Set the sectionKeyPath to "articleSection" so the NSFetchedResultsController creates the sections for you. Something like this:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];;
    request.fetchBatchSize = 20;
    // sort by "articleSection"
    NSSortDescriptor *sortDescriptorCategory = [NSSortDescriptor sortDescriptorWithKey:@"articleSection" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObjects:sortDescriptorCategory, nil];;
    
    // create nsfrc with "articleSection" as sectionNameKeyPath
    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"articleSection" cacheName:@"MyFRCCache"];
    frc.delegate = self;
    NSError *error = nil;
    if (![frc performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    self.fetchedResultsController = frc;
    
    0 讨论(0)
提交回复
热议问题