2012-04-16 6 views
3

UILocalizedIndexedCollationはどのように機能しますか?UILocalizedIndexedCollat​​ionはどのように機能しますか? (ドキュメントのサンプルコード)

私はドキュメントからsimpleIndexedTableViewsample codeを読んでいた、初期化されRootViewControllerは、我々はそれにいくつかのデータを送信 (rootViewController.timeZonesArray = timeZones;)が、この時点では、collationはすでにビューコントローラで使用されます。基本的にこの

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // The number of sections is the same as the number of titles in the collation. 
    return [[collation sectionTitles] count]; 
} 

魔法が起きているようだから、[collation sectionTitles]には何が入っていますか?何かが自動でなければならないが、私は全体の仕組みがどのように働いているのか分からない。

- (void)configureSections { 

    // Get the current collation and keep a reference to it. 
    self.collation = [UILocalizedIndexedCollation currentCollation]; 

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

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

    // Set up the sections array: elements are mutable arrays that will contain the time zones for that section. 
    for (index = 0; index < sectionTitlesCount; index++) { 
     NSMutableArray *array = [[NSMutableArray alloc] init]; 
     [newSectionsArray addObject:array]; 
     [array release]; 
    } 

    // Segregate the time zones into the appropriate arrays. 
    for (TimeZoneWrapper *timeZone in timeZonesArray) { 

     // Ask the collation which section number the time zone belongs in, based on its locale name. 
     NSInteger sectionNumber = [collation sectionForObject:timeZone collationStringSelector:@selector(localeName)]; 

     // Get the array for the section. 
     NSMutableArray *sectionTimeZones = [newSectionsArray objectAtIndex:sectionNumber]; 

     // Add the time zone to the section. 
     [sectionTimeZones addObject:timeZone]; 
    } 

    // Now that all the data's in place, each section array needs to be sorted. 
    for (index = 0; index < sectionTitlesCount; index++) { 

     NSMutableArray *timeZonesArrayForSection = [newSectionsArray objectAtIndex:index]; 

     // If the table view or its contents were editable, you would make a mutable copy here. 
     NSArray *sortedTimeZonesArrayForSection = [collation sortedArrayFromArray:timeZonesArrayForSection collationStringSelector:@selector(localeName)]; 

     // Replace the existing array with the sorted array. 
     [newSectionsArray replaceObjectAtIndex:index withObject:sortedTimeZonesArrayForSection]; 
    } 

    self.sectionsArray = newSectionsArray; 
    [newSectionsArray release]; 
} 

あなたの助け

答えて

4

[collation sectionTitles]ためのおかげで、単純にテーブルビューの右側に沿って表示するためのソート、標準のアルファベットが含まれています。 'B'と 'F'で始まる項目しかない場合でも、側面のインデックスは常にフルアルファベットを表示します。しかし、文字列をソートするために使われる標準のアルファベットはロケールに応じて変わるので、AppleはUILocalizedIndexedCollationを与えて、ユーザーに期待するものを簡単に表示できるようにしました。

+0

BJホーマーにお返事ありがとうございました! – Paul

関連する問題