2011-12-30 3 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; 
} 

を、私はエラーをそれぞれ持っています私は対応するセクションを持たない文字を選択します。だから私がやろうとしているのは、選択された文字を使ってセクションインデックスを検索し、既存のセクションにその位置を検索することです。しかし、それは動作しません。 アイデアはありますか?事前に

おかげで、あなたはfetchedResultController

@property (nonatomic, readonly) NSArray *sectionIndexTitles 

で検索する必要があり yassa

答えて

5

。それは、追加の言語をサポートするために、私あなたがNSNotFoundを受信すると、前の文字のインデックス、または前を返すために、いくつかのロジックを実行する必要があります存在しないインデックス、または以前、など

+1

信じられないほど、私の前にあり、私はそれを見ませんでした! :) あなたが示唆したように、それは次のようにすることで十分でした: 'correspondingIndex = [[fetchedResultsController sectionIndexTitles] indexOfObject:correspondingLetter];'。 ありがとうございました! – yassassin

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]; 
} 
関連する問題