从NSArray为UITableView创建索引

编程入门 行业动态 更新时间:2024-10-24 18:15:55
本文介绍了从NSArray为UITableView创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经读过创建索引的最佳方法(uitableview侧面的az)是设置一个nsdictionaries数组,其中每个字典对应一个部分,而rowValue键包含一个数组行。

I've read that the best way of creating an index (the a-z at the side of a uitableview) is to set up an array of nsdictionaries, where each dictionary corresponds to a section, and a rowValue key contains an array of the rows.

NSDictionary headerTitle => ‘A’ rowValues => {"Aardvark", "Ape", "Aquaman"} NSDictionary headerTitle => ‘B’ rowValues => {"Bat", "Boot", "Bubbles"} etc

但是如何从所有行标题的数组 - {Aardvark,Ape,Aquaman,Bat,Boot,Bubbles,Cat,Cabbage等}}?

But how can this be created from an array of all the row titles - {"Aardvark", "Ape", "Aquaman", "Bat", "Boot", "Bubbles", "Cat", "Cabbage" etc} ...?

推荐答案

#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.... }

这是我在最近的一个项目中使用过的代码。

This is the code that I have used in one of the recent projects.

更多推荐

从NSArray为UITableView创建索引

本文发布于:2023-11-22 18:25:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   NSArray   UITableView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!