2011-06-30 5 views
0

編集:更新されたコードNSDictionaryの

私は、各セクション内のテーブルセクションから辞書と、インデックスリストを作成しようとしていますので、セクションの文字がキーになります...私が持っているセクションの場合:

self.collation = [UILocalizedIndexedCollation currentCollation]; 

NSInteger index, sectionTitlesCount = [[collation sectionTitles] count]; 

NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; 

NSString *alphabet = [[NSString alloc] initWithString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZ"]; 

for (index = 0; index < sectionTitlesCount; index++) { 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [newSectionsArray addObject:array]; 

    NSString *letter = [alphabet substringWithRange:NSMakeRange(index, 1)]; 
    [myDict setObject:newSectionsArray forKey:letter]; 

    [array release]; 
} 

これはでクラッシュ:私はSEのためのデータを追加する必要があり、各文字について次に

'*** -[NSCFString substringWithRange:]: Range or index out of bounds' 

を私はこの配列がセクション/文字キーのオブジェクトであると思います:

for (Data *myData in self.dataModel.datalist) { 

    NSInteger sectionNumber = [collation sectionForObject:myData collationStringSelector:@selector(myName)]; 

    NSMutableArray *sectionData = [newSectionsArray objectAtIndex:sectionNumber]; 

    [sectionData addObject:myData]; 

} 

これは意味があると思います!ありがとう!!!

答えて

0

あなたがまだ表示されない

NSArray *sectionArray = [myData filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [sections objectAtIndex:[indexPath section]]]]; 
+0

これは正直言ってもずっとシンプルなようですが、Appleの例はあいまいな照合ルートで私を送ってくれました!そのフィルタリングされた配列は、フィルタリングされた検索結果に対して機能しますか?どうもありがとう!!! – medley

+0

アップルが 'UILocalizedIndexedCollat​​ion'を使って説明する理由は、ローカリゼーションを助けるためです。上記の手法を使用すると、デバイスの現在の言語に関係なく、常に英語のアルファベットが提供されます。 –

+0

OK、情報ありがとうございます。アップルが見つけたインデックス付きのテーブルビューの唯一のデモだった。 – medley

0

クラッシュ、sectionTitlesCountの値が文字列アルファベットの文字数より大きいことを示唆しています。

私はsectionTitlesCountが表すか(これはあなたのコードには存在しないよう)それがどこから来るかどのような値わからないんだけど、ループのためにあなたのの最初の行を交換する方が良いと思います

for (index = 0; index < alphabet.length; index++) { 
+0

あなたがデータのあなたのサブセクションを得ることができますデリゲートメソッド- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {で次に

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView { return [sections count]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return sections; } 

実装配列

 sections = [NSArray arrayWithObjects:@"#", @"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]; 

であなたのセクションを定義するものsectionTitlesCountの** physical **値は? NSLog(@ "%i"、sectionTitleCount);を設定した場所の直下に、ログにはどのような値が出力されますか? –

+0

ああ、申し訳ありません... "sectionTitlesCount 27"(27回)を返します – medley

+0

英語のアルファベットに26文字しかないため、クラッシュするのはなぜですか。インデックス配列をハードコーディングせずに行うことを示す例[here](http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Reference/UILocalizedIndexedCollat​​ion_Class/UILocalizedIndexedCollat​​ion.html)はありませんか? –

0

セクションを投稿してください。それは文字列の長さよりもgreatorかもしれません。