2011-12-05 12 views
6

ile bölümlere ayırıyor Böylece, bir sunucudan nesneleri almak, kaydetmek ve bir UITableView'da görüntülemek için Çekirdek Verileri başarıyla uygulayabildim. Ancak şimdi bunları ayrı bölümlere ayırmak istiyorum. Şimdi birkaç gün aradım ve NSFetchedResultsController beni işe yarar şekilde kullanıyor olsa bile, kafamı karıştırıyor gibi görünüyor. Öğemde, "Üst" "Spor" "Hayat" gibi öğelerle Temel Verilere eklendiğinde ayarlanan "articleSection" adında bir anahtarım var. Bunları UITableView'imde ayrı bölümlere ayırmaya nasıl başlarım? Birden fazla NSFetchedResultsControllers kullanma hakkında okudum, ancak bununla olabildiğince sinirliyim.iPhone - Çekirdek Verilerini NSFetchResultsController

Her türlü öneri veya yardım büyük takdir edilecektir.

+0

bu konuya http://stackoverflow.com/questions/8037588/sectionindextitlesfortableview-keys-are-off/8037745#8037745 benim yanıta bakın. Bu yardımın umarım. – iamsult

cevap

17

documentation for NSFetchedResultsController mükemmel çalışan bir örnek kod içeriyor. Sonuçlar articleSection göre sıralanır böylece

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

getirme isteğinin sortDescriptors ayarlayın.
SectionKeyPath bölümünü "articleSection" olarak ayarlayın, böylece NSFetchedResultsController sizin için bölümler oluşturur. Böyle bir şey:

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

NSFetchedResultsController'ı kullanmak istemiyorsam bunu nasıl yaparım? – acecapades