Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does UILocalizedIndexedCollation work? (sample code from documentation)

how does UILocalizedIndexedCollation work?

i was reading the simpleIndexedTableView sample code from the documentation, the RootViewController is initialized, we send it some data ( rootViewController.timeZonesArray = timeZones; ) but at this point, collation is already used in the view controller :

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

basically this is where the magic seems to happen, so what does [collation sectionTitles] contain? something must be automatic, but i don't see how the whole thing worked?

- (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]; 
}

Thanks for your help

like image 586
Paul Avatar asked Oct 20 '25 06:10

Paul


1 Answers

[collation sectionTitles] simply contains the standard alphabet sorting for display along the right-hand side of the tableview. Even if you only have items starting with 'B' and 'F', the index on the side always shows the full alphabet. But since the standard alphabet used for sorting strings changes depending on the locale, Apple gave us UILocalizedIndexedCollation to make it easy to show the user what they'd expect.

like image 97
BJ Homer Avatar answered Oct 21 '25 22:10

BJ Homer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!