UILocalizedIndexedCollation
如何工作?
我从文档中读取了simpleIndexedTableView
sample 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];
}
谢谢你的帮助
最佳答案
[collation sectionTitles]
简单地包含标准的字母排序,以便在TabLVIEW的右侧显示。即使只有以“b”和“f”开头的项,旁边的索引也始终显示完整的字母表。但是,由于用于对字符串进行排序的标准字母表会根据地区的不同而有所变化,因此苹果给了我们UILocalizedIndexedCollation
以方便向用户显示他们的期望。