self.collation = [UILocalizedIndexedCollation currentCollation];
NSUInteger sectionTitlesCount = [_collation.sectionTitles count];
self.outerArray = [NSMutableArray arrayWithCapacity:sectionTitlesCount];
for (NSUInteger index = 0; index < sectionTitlesCount; index++)
{ NSMutableArray *array = [NSMutableArray array];
[self.outerArray addObject:array];
}
for (int i = 0; i < [_tableData count]; i++){
NSDictionary *myDic = [_tableData objectAtIndex:i];
NSInteger sectionNumber = [_collation sectionForObject:[myDic objectForKey:@"UserName"] collationStringSelector:@selector(lowercaseString)];
NSMutableArray *sectionNames = [_outerArray objectAtIndex:sectionNumber];
[sectionNames addObject:myDic];
} //用字典替换 string, 字典里是 UserName:人名 OrgName: 部门
// for (NSString *nameString in [_tableData valueForKey:@"UserName"])
// {
// NSInteger sectionNumber = [_collation sectionForObject:nameString collationStringSelector:@selector(lowercaseString)];
// NSMutableArray *sectionNames = [_outerArray objectAtIndex:sectionNumber];
// [sectionNames addObject:nameString];
// } 用上边的替换了。
for (NSUInteger index = 0; index < sectionTitlesCount; index++)
{
NSMutableArray *namesForSection = [_outerArray objectAtIndex:index];
NSMutableArray *mkNameArray = [[NSMutableArray alloc]init];
for (int i = 0; i< [namesForSection count]; i++){
NSString *nameString = [[namesForSection objectAtIndex:i]objectForKey:@"UserName"];
[mkNameArray addObject:nameString];
}
NSMutableArray *sortedNamesForSection = [NSMutableArray arrayWithArray:[_collation sortedArrayFromArray:mkNameArray collationStringSelector:@selector(lowercaseString)]];
[mkNameArray release];
int m;
for (m = 0; m < [sortedNamesForSection count]; m++){
NSString *sortedName = [sortedNamesForSection objectAtIndex:m];
for (int n =0; n< [namesForSection count]; n++){
NSString *namesInDic = [[namesForSection objectAtIndex:n]objectForKey:@"UserName"];
if ([sortedName isEqualToString:namesInDic]){
[sortedNamesForSection replaceObjectAtIndex:m withObject:[namesForSection objectAtIndex:n]];
}
}
}
[self.outerArray replaceObjectAtIndex:index withObject:sortedNamesForSection];
}添加了副标题, 每个分区内的排序要重新设计