设置内容IOS开发(51)之UITableView建立索引搜索
废话就不多说了,开始。。。
1 言前
昨天任务时候碰到TableView的建立索引问题,由于有的时候TableView中之的数据量非常之大,以至于需要在侧右建立索引来搜索,日今意特理整于上面,供大家参考,互相学习。
2 代码实例
ZYViewController.h
#import <UIKit/UIKit.h> @interface ZYViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate> //设置索引题标 @property(nonatomic,retain)NSMutableArray *indexArray; //设置每一个section下的cell内容 @property(nonatomic,retain)NSArray *dataArray1; @property(nonatomic,retain)NSArray *dataArray2; @property(nonatomic,retain)NSArray *dataArray3; @end
ZYViewController.m
@synthesize dataArray1,dataArray2,dataArray3;
@synthesize indexArray;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array1=[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E" ,nil];
NSArray *array2=[[NSArray alloc] initWithObjects:@"F",@"G",@"H",@"I",@"J", nil];
NSArray *array3=[[NSArray alloc] initWithObjects:@"K",@"L",@"M",@"N",@"O", nil];
self.dataArray1=array1;
self.dataArray2 = array2;
self.dataArray3 = array3;
//给组数值赋
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:5];
self.indexArray = array;
[array release];
[indexArray addObjectsFromArray:array1];
[indexArray addObjectsFromArray:array2];
[indexArray addObjectsFromArray:array3];
[array1 release];
[array2 release];
[array3 release];
}
//设置Section的Header的值
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section {
NSString *key = [indexArray objectAtIndex:section];
return key;
}
#pragma mark -
#pragma mark Table View Data Source Methods
//设置表格的索引组数
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexArray;
}
////答应数据源告诉必须加载到Table View中的表的Section数。
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
//设置表格的行数为组数的素元个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section==0) {
return [self.dataArray1 count];
}else if(section==1)
return dataArray2.count;
else
return dataArray3.count;
}
//每一行的内容为组数应相索引的值
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if(indexPath.section==0)
//设置单元格的字符串内容
cell.textLabel.text=[self->dataArray1 objectAtIndex:indexPath.row];
else if(indexPath.section==1)
//设置单元格的字符串内容
cell.textLabel.text=[self->dataArray2 objectAtIndex:indexPath.row];
else
//设置单元格的字符串内容
cell.textLabel.text=[self->dataArray3 objectAtIndex:indexPath.row];
return cell;
}
-(void)dealloc{
[indexArray release];
[dataArray1 release];
[dataArray2 release];
[dataArray3 release];
[super dealloc];
}
行运结果:
3 结语
以上就是全部内容,望希对大家有所帮助
Demo下载地址:http://download.csdn.net/detail/u010013695/5347887
文章结束给大家分享下程序员的一些笑话语录:
一个程序员对自己的未来很迷茫,于是去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问Lippman,他现在领导的程序员的队伍可能是地球上最大的"
于是他去问Lippman。
Lippman说"程序员的未来就是驾驭程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问Gates,他现在所拥有的财产可能是地球上最多的"
于是他去问Gates。
Gates说"程序员的未来就是榨取程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝说"我的孩子,你去问侯捷,他写的计算机书的读者可能是地球上最多的"
于是他去问侯捷。
侯捷说"程序员的未来就是诱惑程序员"
这个程序员对这个未来不满意,于是他又去问上帝。
"万能的上帝呀,请你告诉我,我的未来会怎样?"
上帝摇摇头"唉,我的孩子,你还是别当程序员了")

浙公网安备 33010602011771号