8
創建表部分我用NSFetchedResultsController
驅動數據爲我UITableViewController
。被管理對象的簡化版本我取這個樣子的(井爲例):與NSFetchedResultsController
Person:
-(BOOL)hasPet;
-(BOOL)hasDog;
基本上,我希望我的表格只顯示那些Person
對象誰擁有的寵物。這很簡單,我可以使用謂詞。現在,對於那些誰hasPet == YES
,我想將它們放在表中2個部分,第一部分hasDog == YES
,第二是hasDog == NO
。這是我有點模糊。這裏是我配置結果控制器的地方,希望有人能幫助我指引正確的方向。
- (NSFetchedResultsController *)fetchedResultsController {
if (nil != fetchedResultsController) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Homework" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hasPet == %@", [NSNumber numberWithBool:YES]];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *dogDescriptor = [[NSSortDescriptor alloc] initWithKey:@"hasDog" ascending:YES];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:dogDescriptor, sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aController.delegate = self;
self.fetchedResultsController = aController;
// Release things
return fetchedResultsController;
}
是一種描述,我必須專注我的注意?