2012-04-16 76 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]只包含标准的字母排序沿的tableview的右侧显示。即使您只有以'B'和'F'开头的项目,旁边的索引始终显示完整的字母表。但由于用于排序字符串的标准字母表根据语言环境而改变,Apple给了我们UILocalizedIndexedCollation以便向用户展示他们期望的内容。

+0

非常感谢你的答案BJ荷马! – Paul 2012-04-17 21:40:16

相关问题