Jon*_*nah 1 arrays sorting iphone uitableview
我正在努力将普通的tableview转换为分段的tableview.我希望节标题是该节中项目的第一个字母.
这是我目前在titleForHeaderInSection方法中所拥有的:
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"Name"
ascending:YES] autorelease];
NSArray *sortedArray;
NSMutableArray *commonNameArray = [tableDataSource valueForKey:@"Name"];
NSArray *uniquearray;
uniquearray = [[NSSet setWithArray:commonNameArray] allObjects];
sortedArray = [uniquearray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
Run Code Online (Sandbox Code Playgroud)
此代码删除重复的标题并按字母顺序对列表进行排序.现在我只需要将数组中的字符串转换为第一个字母.最好的方法是什么?
NSMutableSet *firstCharacters = [NSMutableSet setWithCapacity:0];
for( NSString *string in [tableDataSource valueForKey:@"Name"] )
[firstCharacters addObject:[string substringToIndex:1]];
NSArray *uniquearray = [[firstCharacters allObjects] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
Run Code Online (Sandbox Code Playgroud)