2011-12-30 35 views
5

我已經實現了一個填充了核心數據的表格,現在我試圖通過顯示邊字母表(像格式的聯繫人)來爲它建立索引。核心數據中的部分索引的整個字母表

在下面的代碼中,如果使用註釋行,我只有現有節的字母。但我想要整個字母表,所以我更改了返回的數組:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{  
    //return [fetchedResultsController sectionIndexTitles];  

    indexArray = [NSArray arrayWithObjects: @"{search}", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J",@"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", @"#", nil]; 
    return indexArray; 
} 

所有字母都顯示在邊索引上。但現在我已經實現返回所選部分的參數指標的方法,在這裏我有一些問題:

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    //return [fetchedResultsController sectionForSectionIndexTitle:title atIndex:index]; 

    NSString *correspondingLetter = [indexArray objectAtIndex:index]; 
    NSUInteger correspondingIndex = [[fetchedResultsController sections] indexOfObject:correspondingLetter]; 

    NSLog(@"------index:%i\ncorrespondingLetter: %@\ncorrespondingIndex: %i\n", index,correspondingLetter, correspondingIndex); 

    return correspondingIndex; 
} 

有了上面的代碼,如果我使用註釋行,我有每一個錯誤我選擇一個沒有相應部分的信件。所以我試圖做的是使用已選定的字母檢索部分索引並在現有部分中搜索其位置。但它不起作用。 你有什麼想法嗎?

由於提前, yassa

回答

5

你應該

@property (nonatomic, readonly) NSArray *sectionIndexTitles 
fetchedResultController

搜索。
但是,如果它不存在,您將收到NSNotFound,需要做一些邏輯返回先前的字母索引,或前,或前一個指數等

+1

令人難以置信,它在我面前,我沒有看到它! :) 正如你所建議的,它足以執行以下操作: 'correspondingIndex = [[fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];'。 非常感謝! – yassassin 2011-12-30 19:31:38

2

以支持更多的語言我不會使用硬編碼字符。我的解決方案受到公認的答案的啓發:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; 
} 


- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
    NSString *correspondingLetter = [[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles] objectAtIndex:index]; 
    return [[self.fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter]; 
}