我有一个名字数组,我想把章节标题作为字母表,如A B C.还有索引。什么是从一系列名称中制作章节的最有效方法
答案 0 :(得分:2)
以下是我开发的处理NSArray
并创建NSDictionary
的方法。
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray* internalArray = [NSMutableArray arrayWithObjects:@"abc",@"xyz",@"aa",@"bb", nil];
NSDictionary* dictStructued = [self sortArrayAndMakeStructur:internalArray];
NSLog(@"Output : %@",dictStructued.description);
}
这是你的整个方法..
-(NSMutableDictionary*)sortArrayAndMakeStructur:(NSArray*)array{
NSMutableDictionary* dictIndexes = [[NSMutableDictionary alloc]init];
NSArray* sortedArray = [array sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
for (int i=0; i<[sortedArray count]; i++) {
NSString* strName = sortedArray[i];
if ([[dictIndexes allKeys] containsObject:[[strName substringToIndex:1] uppercaseString]]) {
NSMutableArray* internalArray = [NSMutableArray arrayWithArray:[dictIndexes valueForKey:[[strName substringToIndex:1] uppercaseString]]];
[internalArray addObject:strName];
[dictIndexes setObject:internalArray forKey:[[strName substringToIndex:1] uppercaseString]];
}
else{
NSMutableArray* internalArray = [NSMutableArray arrayWithObjects:strName, nil];
[dictIndexes setObject:internalArray forKey:[[strName substringToIndex:1] uppercaseString]];
}
}
return dictIndexes;
}
此处的节数= [[dictStructued allKeys] count];
行数= [[dictIndexes valueForKey:yourKey] count];
输出:
{
A = (
aa,
abc
);
B = (
bb
);
X = (
xyz
);
}
答案 1 :(得分:0)
请使用带有每个键的数组的嵌套NSArray或NSDictionary。
引用自: http://www.appcoda.com/ios-programming-index-list-uitableview/
答案 2 :(得分:0)
首先,我们用字母对它们进行排序:
NSMuatableArray* sortedArray = [anArray sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
我们会在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
希望这可以提供帮助。