我读过,创建一个索引的最佳方法(在uitableview一侧的a-z)是设置一个nsdiction数组,每个字典对应一个section,一个rowValue键包含一个数组.
NSDictionary headerTitle => ‘A’ rowValues => {”Aardvark”,“Ape”,“Aquaman”} NSDictionary headerTitle => ‘B’ rowValues => {”Bat”,“Boot”,“Bubbles”} etc
但是,可以从所有行标题 – “{Aardvark”,“Aquaman”,“Bat”,“Bubbles”,“Cat”,“Cabbage”等)的数组中创建. ..?
解决方法
#pragma mark - #pragma mark View lifecycle - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *temp = [[NSMutableArray alloc] init]; NSMutableArray *temp2 = [[NSMutableArray alloc] init]; for(int i = 0; i < tableListArray.count; i++) { Nsstring *string = [tableListArray objectAtIndex:i]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; [dict setobject:string forKey:@"Name"]; [dict setobject:[NSNumber numberWithInt:i] forKey:@"ID"]; Nsstring *firstString = [string substringToIndex:1]; if([temp2 containsObject:firstString] == NO || temp2.count == 0) { if(temp2.count != 0) { [temp addobject:temp2]; [temp2 release]; temp2 = [[NSMutableArray alloc] init]; } [temp2 addobject:firstString]; } [temp2 addobject:dict]; [dict release]; } [temp addobject:temp2]; detailListArray = [[NSArray alloc] initWithArray:temp]; [temp release]; [temp2 release]; } #pragma mark - #pragma mark Table view data source - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(Nsstring *)title atIndex:(NSInteger)index { int i = 0; for(NSArray *array in detailListArray) { Nsstring *string = [array objectAtIndex:0]; if([string compare:title] == NSOrderedSame) break; i++; } return i; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return detailListArray.count; } - (Nsstring *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSArray *array = [detailListArray objectAtIndex:section]; return [array objectAtIndex:0]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { NSMutableArray *titleArray = [NSMutableArray array]; [titleArray addobject:@"A"]; [titleArray addobject:@"B"]; [titleArray addobject:@"C"]; [titleArray addobject:@"D"]; [titleArray addobject:@"E"]; [titleArray addobject:@"F"]; [titleArray addobject:@"G"]; [titleArray addobject:@"H"]; [titleArray addobject:@"I"]; [titleArray addobject:@"J"]; [titleArray addobject:@"K"]; [titleArray addobject:@"L"]; [titleArray addobject:@"M"]; [titleArray addobject:@"N"]; [titleArray addobject:@"O"]; [titleArray addobject:@"P"]; [titleArray addobject:@"Q"]; [titleArray addobject:@"R"]; [titleArray addobject:@"S"]; [titleArray addobject:@"T"]; [titleArray addobject:@"U"]; [titleArray addobject:@"V"]; [titleArray addobject:@"W"]; [titleArray addobject:@"X"]; [titleArray addobject:@"Y"]; [titleArray addobject:@"Z"]; return titleArray; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSArray *array = [detailListArray objectAtIndex:section]; return (array.count - 1); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELL"] autorelease]; NSArray *array = [detailListArray objectAtIndex:indexPath.section]; NSDictionary *dict = [array objectAtIndex:indexPath.row + 1]; cell.textLabel.text = [dict objectForKey:@"Name"]; return cell; } #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *array = [detailListArray objectAtIndex:indexPath.section]; NSDictionary *dict = [array objectAtIndex:indexPath.row + 1]; int entryID = [[dict objectForKey:@"ID"] intValue]; // Do what ever you want to do with the selected row here.... }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。