IOS开发(51)之UITableView建立索引搜索
来源:互联网 发布:osi网络层 编辑:程序博客网 时间:2024/05/22 01:33
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 *)tableViewtitleForHeaderInSection:(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
- IOS开发(51)之UITableView建立索引搜索
- IOS开发(52)之UITableView索引搜索之UILocalizedIndexedCollation
- ios开发之uiTableView增加搜索框
- iOS 之 UITableView 使用索引
- IOS开发(23)之UITableView控件
- iOS开发之UITableView(一)
- iOS UITableView索引列开发详解
- iOS UITableView加索引条和搜索框Demo
- iOS开发 UITableView之cell
- iOS开发学习之UITableView
- 4、iOS 开发之 UITableView
- ios开发系列之UITableView
- iOS开发之UITableView多选
- iOS开发之UITableView详解
- iOS开发之自定义UITableView
- iOS开发之高级视图—— UITableView(五)表视图索引
- iOS UITableView索引选择
- iOS开发 之 搜索
- duilib的一些bug
- Google Tag Manager 入门指南
- Android拍照、录像、录音代码范例
- VS2010 如何缩减编译文件大小(statically linked MFC)
- 五大常用算法(二)动态规划
- IOS开发(51)之UITableView建立索引搜索
- 未来的竞争已不在行业中, 而是在行业外!!
- YUV420-RGB 以点的方式进行转换 原创by: GanYJ
- 内存映射修改大文件
- Color depth (from Wiki)
- php 下的测试工具Xdebug的安装及使用
- android ViewTreeObserver详细讲解
- openstack nova基础知识——RabbitMQ
- 桥接模式