我想知道是否有人知道最好的解決方案來定義UITableView中的部分自定義顯示順序 - 我使用核心數據和NSFetchedResultsControler加載數據,我成功地使用NSSortDescriptors,併成功地將返回的項使用sectionNameKeyPath的部分:如何更改從NSFetchedResultsControler填充的UITableview中各部分的顯示順序?
到目前爲止很好但是 - 這些部分按字母順序列出,我想重寫此部分並按特定的自定義排序順序對部分進行排序。
例如,如果我有一個屬性,叫做顏色的實體,稱爲項目,我用這個顏色屬性作爲sectionNameKeyPath:我怎麼會再下訂單的部分以特殊的方式 - 目前部分顯示爲
藍, Green, Indigo, Orange, Red, Violet, Yellow。
- 因爲他們有NSortDescriptor應用於NSFetchedResultsController(我已經定義)。但我怎麼能在顯示部分,像這樣一種特定的方式重新排列這些部分 -
紅,橙 , 黃,綠 , 藍,靛藍 , 紫。
我NSFetchedResultsController代碼如下 -
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController_ != nil) {
return fetchedResultsController_;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *colourDescriptor = [[NSSortDescriptor alloc] initWithKey:@"colour" ascending:YES];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:colourDescriptor, sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"colour" cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[colourDescriptor release];
[sortDescriptors release];
NSError *error = nil;
if (![fetchedResultsController_ performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return fetchedResultsController_;
}
我明白貼使用NSManagedObject的自定義子類和getter方法來動態分配屬性被用作sectionNameKeyPath有關的例子:對於這些,但例子仍然傾向於提供部分名稱,然後按字母順序列出。
很明顯,我剛剛接觸這個新手,但任何建議/方向都將不勝感激。
非常感謝,謝謝,我會潛入並給出一個去。 – user432841 2010-08-27 17:43:38