6

したがって、サーバーからオブジェクトを取得して保存し、それらをUITableViewに表示するためにコアデータを正常に実装しました。しかし今、これらを別々のセクションに分割したいと思います。私は数日を探しましたが、NSFetchedResultsControllerは私がそれを使用している方法でも、私を混乱させるようです。 Entityには、 "Top" "Sports" "Life"などの項目を持つCore Dataに項目が追加されたときに設定される "articleSection"というキーがあります。私はどのように私のUITableViewの別のセクションにこれらを壊すに行くだろうか?私は複数のNSFetchedResultsControllersの使用について読んだことがありますが、私はこれと同じくらい不満を抱いています。iPhone - コアデータをNSFetchResultsControllerのセクションに分割する

ご意見やご協力をいただければ幸いです。

+0

がこのスレッドhttp://stackoverflow.com/questions/8037588/sectionindextitlesfortableview-keys-are-off/8037745#8037745に私の答えを参照してください。 このヘルプが必要です。 – iamsult

答えて

17

documentation for NSFetchedResultsControllerには完全に動作するサンプルコードがあります。結果はarticleSectionでソートされるように、フェッチ要求のsortDescriptorsを設定し

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


sectionKeyPathを "articleSection"に設定すると、NSFetchedResultsControllerがセクションを作成します。このような何か:

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を使用したくない場合、どうすればいいですか? – acecapades

関連する問題