3

どのように機能しUILocalizedIndexedCollationますか?

私はドキュメントからsimpleIndexedTableView サンプルコードを読んでいました、RootViewController初期化され、それにいくつかのデータ(rootViewController.timeZonesArray = timeZones;)を送信しますが、この時点で、collationViewControllerですでに使用されています:

- (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

1 に答える 1

5

[collation sectionTitles]テーブルビューの右側に表示するための標準のアルファベットの並べ替えが含まれているだけです。「B」と「F」で始まるアイテムしかない場合でも、側面のインデックスには常に完全なアルファベットが表示されます。ただし、文字列の並べ替えに使用される標準のアルファベットはロケールによって異なるため、AppleはUILocalizedIndexedCollation、ユーザーが期待するものを簡単に表示できるようにするために提供してくれました。

于 2012-04-16T17:30:20.610 に答える