如何使数组的uitableview索引和部分
我有一个名称数组,我想把部分标题作为字母,如AB C.也索引。 什么是使一个名称arrays中最有效的方法
这里是我开发的方法在NSArray
上处理,并创build您所预期的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; }
这里的section count = [[dictStructued allKeys] count];
Number Of Row = [[dictIndexes valueForKey:yourKey] count];
输出 :
{ A = ( aa, abc ); B = ( bb ); X = ( xyz ); }
请使用与每个键都有数组的嵌套NSArray或NSDictionary。
引自: http : //www.appcoda.com/ios-programming-index-list-uitableview/
首先,我们用字母sorting:
NSMuatableArray* sortedArray = [anArray sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)];
我们通常在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
希望这可以帮助。