Iphone UILocalizedIndexedCollation是如何工作的?(文档中的示例代码)
Iphone UILocalizedIndexedCollation是如何工作的?(文档中的示例代码),iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,UILocalizedIndexedCollation如何工作 我正在阅读文档中的simpleIndexedTableView,初始化了RootViewController,我们向它发送了一些数据(RootViewController.timeZonesArray=timeZones;),但此时,排序规则已在视图控制器中使用: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // The number
UILocalizedIndexedCollation
如何工作
我正在阅读文档中的simpleIndexedTableView
,初始化了RootViewController
,我们向它发送了一些数据(RootViewController.timeZonesArray=timeZones;
),但此时,排序规则已在视图控制器中使用:
- (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];
}
-(无效)配置部分{
//获取当前排序规则并保留对它的引用。
self.collation=[UILocalizedIndexedCollation currentCollation];
NSInteger索引,sectionTitlesCount=[[collation sectionTitles]count];
NSMutableArray*newSectionsArray=[[NSMutableArray alloc]initWithCapacity:sectionTitlesCount];
//设置节数组:元素是可变数组,将包含该节的时区。
对于(索引=0;索引<节标题计数;索引++){
NSMUTABLEARRY*array=[[NSMUTABLEARRY alloc]init];
[newSectionsArray添加对象:数组];
[阵列释放];
}
//将时区分隔为适当的阵列。
用于(时区包装器*时区阵列中的时区){
//根据时区的区域名称,询问排序规则该时区属于哪个区号。
NSInteger sectionNumber=[collation sectionForObject:timeZone collationStringSelector:@selector(localeName)];
//获取节的数组。
NSMutableArray*sectionTimeZones=[newSectionsArray对象索引:sectionNumber];
//将时区添加到节中。
[分区时区添加对象:时区];
}
//现在所有数据都已就绪,需要对每个节数组进行排序。
对于(索引=0;索引<节标题计数;索引++){
NSMutableArray*timeZonesArrayForSection=[newSectionsArray对象索引:索引];
//如果表视图或其内容是可编辑的,则可以在此处创建可变副本。
NSArray*sortedTimeZonesArrayForSection=[排序规则SortedDarrayFromArray:timeZonesArrayForSection排序规则字符串选择器:@选择器(localeName)];
//用已排序的数组替换现有数组。
[newSectionsArray replaceObjectAtIndex:index with Object:SortedTimeZoneArrayForSection];
}
self.sectionsArray=newSectionsArray;
[新闻发布];
}
感谢您的帮助[collation sectionTitles]
仅包含标准的字母排序,以便在tableview的右侧显示。即使只有以“B”和“F”开头的项目,侧面的索引也始终显示完整的字母表。但是,由于用于对字符串进行排序的标准字母表会随着地区的不同而变化,苹果公司为我们提供了UILocalizedIndexedCollation
,以便向用户展示他们的期望