我无法在 UITableView 中制作这些部分。我查看了文档UILocalizedIndexedCollation
以及此示例代码项目:
我下面的内容基本上是来自示例项目的直接复制/粘贴。但是,示例项目使用自定义对象 (TimeZoneWrapper.h),然后根据对象的实例变量 (TimeZoneWrapper.localeName) 将对象放置在正确的部分中。但是,我没有使用自定义对象。我只使用了一堆常规的 NSStrings。所以我的问题是我应该将 NSString 上的什么方法传递给 @selector() 来比较字符串并将其放置在正确的节数组中?
目前,我正在调用 NSString 的复制方法作为一种临时技巧,以使事情正常工作(确实如此),但我不确定这是否正确。一点解释将不胜感激!
- (void)configureSections {
// Get the current collation and keep a reference to it.
self.collation = [UILocalizedIndexedCollation currentCollation];
NSInteger index, sectionTitlesCount = [[collation sectionTitles] count]; // sectionTitles are A, B, C, etc.
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
// Set up the sections array: elements are mutable arrays that will contain the locations for that section.
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
// Segregate the loctions into the appropriate arrays.
for (NSString *location in locationList) {
// Ask the collation which section number the location belongs in, based on its locale name.
NSInteger sectionNumber = [collation sectionForObject:location collationStringSelector:@selector(/* what do I put here? */)];
// Get the array for the section.
NSMutableArray *sectionLocations = [newSectionsArray objectAtIndex:sectionNumber];
// Add the location to the section.
[sectionLocations addObject:location];
}
// Now that all the data's in place, each section array needs to be sorted.
for (index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *locationsArrayForSection = [newSectionsArray objectAtIndex:index];
// If the table view or its contents were editable, you would make a mutable copy here.
NSArray *sortedLocationsArrayForSection = [collation sortedArrayFromArray:locationsArrayForSection collationStringSelector:@selector(/* what do I put here */)];
// Replace the existing array with the sorted array.
[newSectionsArray replaceObjectAtIndex:index withObject:sortedLocationsArrayForSection];
}
self.sectionsArray = newSectionsArray;
}
提前致谢!