6

Così ho implementato con successo i dati di base per recuperare oggetti da un server, salvarli e visualizzarli in un UITableView. Comunque ora desidero suddividerli in sezioni separate. Ho cercato per qualche giorno, e NSFetchedResultsController sembra confondermi, anche se il modo in cui sto usando funziona. Ho una chiave nella mia entità chiamata "articleSection" che viene impostata quando l'elemento viene aggiunto a Core Data con elementi come "Top" "Sport" "Vita". Come farei a rompere questi in sezioni separate nel mio UITableView? Ho letto sull'utilizzo di più NSFetchedResultsControllers, ma sono frustrato quanto può esserlo.iPhone: suddivisione dei dati principali in sezioni con NSFetchResultsController

Qualsiasi suggerimento o aiuto sarebbe molto apprezzato.

+0

Vedere la mia risposta su questo thread http://stackoverflow.com/questions/8037588/sectionindextitlesfortableview-keys-are-off/8037745#8037745. Spero che questo aiuto. – iamsult

risposta

17

Il codice di esempio documentation for NSFetchedResultsController funziona perfettamente.

- (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]; 
} 

Impostare i sortDescriptors della richiesta di recupero in modo che i risultati sono ordinati per articleSection.
Impostare sectionKeyPath su "articleSection" in modo che NSFetchedResultsController crei le sezioni per l'utente. Qualcosa di simile a questo:

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

Come faccio a fare questo se non voglio usare NSFetchedResultsController? – acecapades

Problemi correlati