6

그래서 코어 데이터를 구현하여 서버에서 객체를 검색하고 저장 한 다음 UITableView에 표시했습니다. 그러나 지금, 나는 이것을 별도의 섹션으로 나누고 싶다. 나는 며칠 동안 찾았고, NSFetchedResultsController는 나를 혼란스럽게 만들었다. 비록 내가 그것을 사용하고있는 방식이 작동하더라도. 엔티티에 "top" "Sports" "Life"와 같은 항목이있는 코어 데이터에 항목이 추가 될 때 설정되는 "articleSection"이라는 키가 있습니다. UITableView에서 이러한 섹션을 개별 섹션으로 분리하는 방법은 무엇입니까? NSFetchedResultsControllers를 여러 개 사용하는 방법에 대해 읽었지 만, 나는 이것과 함께 할 수있는 것처럼 좌절감을 느낍니다.iPhone - NSFetchResultsController를 사용하여 코어 데이터를 섹션으로 분할

모든 제안이나 도움을 주시면 대단히 감사하겠습니다.

+0

이 스레드 http://stackoverflow.com/questions/8037588/sectionindextitlesfortableview-keys-are-off/8037745#8037745에 내 대답을 참조하십시오. 도움이 되길 바랍니다. – iamsult

답변

17

documentation for NSFetchedResultsController에는 완벽하게 작동하는 샘플 코드가 있습니다. 결과가 articleSection으로 분류되어 있으므로

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

는 가져 오기 요청의 sortDescriptors을 설정합니다.
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

관련 문제