2011-12-05 10 views
6

Vì vậy, tôi đã triển khai thành công Dữ liệu cốt lõi để truy xuất các đối tượng từ máy chủ, lưu chúng và hiển thị chúng trong UITableView. Tuy nhiên, bây giờ, tôi muốn chia chúng thành các phần riêng biệt. Tôi đã tìm một vài ngày nay, và NSFetchedResultsController dường như gây nhầm lẫn cho tôi, mặc dù cách tôi đang sử dụng nó hoạt động. Tôi có một chìa khóa trong thực thể của tôi được gọi là "articleSection" được thiết lập khi mục được thêm vào dữ liệu lõi với các mục như "Top" "Thể thao" "Life". Làm thế nào tôi sẽ đi về việc chia chúng thành các phần riêng biệt trong UITableView của tôi? Tôi đã đọc về việc sử dụng nhiều NSFetchedResultsControllers, nhưng tôi thất vọng về điều này.iPhone - chia nhỏ Dữ liệu chính thành các phần với NSFetchResultsController

Mọi đề xuất hoặc trợ giúp sẽ được đánh giá cao.

+0

Xem câu trả lời của tôi về chủ đề này http://stackoverflow.com/questions/8037588/sectionindextitlesfortableview-keys-are-off/8037745#8037745. Hy vọng sự trợ giúp này. – iamsult

Trả lời

17

documentation for NSFetchedResultsController có mã mẫu hoạt động hoàn hảo.

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

Đặt sortDescriptors được yêu cầu lấy để kết quả được sắp xếp theo articleSection.
Đặt phầnKeyPath thành "articleSection" để NSFetchedResultsController tạo các phần cho bạn. Một cái gì đó như thế này:

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

Làm cách nào để thực hiện việc này nếu tôi không muốn sử dụng NSFetchedResultsController? – acecapades