2010-08-27 29 views
1

我想知道是否有人知道最好的解決方案來定義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有關的例子:對於這些,但例子仍然傾向於提供部分名稱,然後按字母順序列出。

很明顯,我剛剛接觸這個新手,但任何建議/方向都將不勝感激。

回答

1

要提供一個自定義的排序順序,您需要子類NSFetchedResultsController

NSFetchedResultsController類文檔:

,如果你想定製段和索引標題的創作,你創建這個類的子類。您覆蓋 sectionIndexTitleForSectionName:if 您希望段落索引標題爲 以外的部分名稱的首字母大寫 。您 覆蓋sectionIndexTitles如果 想索引的標題是什麼 其他比 調用 sectionIndexTitleForSectionName創建數組:上 所有已知的部分。

這是一種隱藏的方式,所以你錯過了你第一次讀到關於這堂課的幾次。

要實現,獲取獲取的結果控制器的section屬性,該屬性是按字母順序排序的節名稱數組,然後返回tableview節索引的正確元素。

+0

非常感謝,謝謝,我會潛入並給出一個去。 – user432841 2010-08-27 17:43:38

0

而不是存儲字符串「紅色」,「橙色」,「黃色」等,存儲代表顏色的枚舉值。因此,將0存儲爲紅色,將1存儲爲橙色等,然後​​將其正確排序。當你想顯示該部分的頭部時,使用枚舉找出它是哪種顏色並找到正確的字符串。