iOS UITableView(九) 给tableView添加索引
2016-01-06 17:28
375 查看
本文介绍如何给tableView添加右侧索引主要是用到了下面的方法
下面展示我的全代码
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{}
下面展示我的全代码
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> { UITableView *_tableView; NSMutableArray *_dataArr; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self creatData]; _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped]; _tableView.delegate=self; _tableView.dataSource=self; [self.view addSubview:_tableView]; } -(void)creatData{ _dataArr=[[NSMutableArray alloc]init]; for (int i=0; i<26; i++) { NSMutableArray *arr=[[NSMutableArray alloc]init]; for (int j=0; j<10; j++) { NSString *str=[NSString stringWithFormat:@"这是第%c组的第%d个标题",'A'+i,j]; [arr addObject:str]; } [_dataArr addObject:arr]; } } #pragma mark -表格索引 //返回右侧索引标题数组 //这个标题的内容时和分区标题相对应 -(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{ NSMutableArray *arr=[[NSMutableArray alloc]init]; //创建26个索引标题 //标题尽量和分区相对应 [arr addObject:UITableViewIndexSearch]; for (int i = 0; i < 26; i++) { NSString *str = [NSString stringWithFormat:@"%c",'A'+i]; [arr addObject:str]; } return arr; } //设置 右侧索引标题 对应的分区索引 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { //cell右侧标题 NSLog(@"title:%@",title); //右侧标题在右侧的索引 NSLog(@"index:%ld",index); //返回 对应的分区索引 return index-1; } //cell 内容的向右缩进 级别 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath { return 1; } #pragma mark uitableView代理 //返回多少组 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return _dataArr.count; } //每个分区有多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [_dataArr[section] count]; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //创建复用标识符 static NSString *cellID =@"cellID"; UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } //填充cell cell.textLabel.text=_dataArr[indexPath.section][indexPath.row]; return cell; } //设置头标 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [NSString stringWithFormat:@"这是第%c组",'A'+section]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end下面是效果
相关文章推荐
- 获取UIImage中的图像数据
- UITableView中visibleCells方法的使用
- IOS将UIView转化为UIImage
- [UE4]UMG、HUI、Slate之间的区别
- ios9新特性UIStackView
- LinkedBlockingQueue
- ueditor表格边框没有颜色的解决
- FlatBuffers文档-Building
- 【iOS开发】关于显示一连串图片组成动画效果UIImageView的使用
- BlockingQueue!
- UILabel 的使用,属性详解
- [bug] Field 'id' doesn't have a default value
- UITableView - 重用机制
- mysql DECLARE CONTINUE HANDLER FOR NOT FOUND
- 13 UIGestureRecognizer and UIMenuController
- MySQL SQL优化案例:相关子查询(dependent subquery)优化
- Android 之UI自适应解决方案
- UITextView: 响应键盘的 return 事件
- android消息处理机制学习(一)-Handler,Message,MessageQueue,Looper简介
- [转] easyui 获取数据表格中选中行的数据 Get selected row data from...