tableview
2019-03-18 22:00
120 查看
1.tableview有代理和源,主要通过代理和源对其进行设置,有很多系统定义的函数可以为其服务
2.tableview就是选项表,就跟外卖那种一样
#import "ViewController.h" //设置代理协议 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{ NSArray *arr;//设置每一行的显示内容 NSArray *arr1; NSArray *arr2; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. arr=[[NSArray alloc] initWithObjects:@"A",@"B",@"C", nil]; arr1=[[NSArray alloc] initWithObjects:@"A1",@"B1",@"C1", nil]; arr2=[[NSArray alloc] initWithObjects:@"A2",@"B",@"C2", nil]; UITableView *mytableviw = [[UITableView alloc] init]; mytableviw.frame=self.view.frame; mytableviw.delegate=self;//代理源 mytableviw.dataSource=self;//数据源 mytableviw.tableFooterView=[[UIView alloc] init]; [self.view addSubview:mytableviw]; }
设置一共有多少行
//两个必须要设置的代理方法:一个设置一共有多少个数据,另一个显示每个数据的内容 //显示多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if(section==0){//section设置来自哪一分组 return arr.count; } else if(section==1){ return arr1.count; } return arr2.count; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;//设置多少组数据 }
设置每个对象的内容
//列表中每一个view对象,设置每一个view的内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellid=@"mycell"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid]; if(cell==nil){ cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid]; } // cell.textLabel.text=[NSString stringWithFormat:@"第%ld行数据",indexPath.row]; if(indexPath.section==0){ cell.textLabel.text=arr[indexPath.row]; } else if(indexPath.section==1){ cell.textLabel.text=arr1[indexPath.row]; } else{ cell.textLabel.text=arr2[indexPath.row]; } return cell; }
设置点击事件
添加表头和表尾
//点击事件 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger cellnum=indexPath.row; // NSLog(@"我点击了第%ld行",cellnum); NSLog(@"我点击了第%@",arr[cellnum]); } //添加一个表头 -(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UILabel *header=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];//这个高度不会影响控件高度要单独设置代理 header.backgroundColor=[UIColor blueColor]; header.text=[NSString stringWithFormat:@"%ld",section]; header.textColor=[UIColor whiteColor]; return header; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 50; } ////添加一个表尾 //-(UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{ // if(section!=2) return nil; // UILabel *header=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 100)];//这个高度不会影响控件高度要单独设置代理 // header.backgroundColor=[UIColor blueColor]; // header.text=@"这是尾部"; // header.textColor=[UIColor whiteColor]; // return header; //} //-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ // return 50; //} @end
相关文章推荐
- iOS tableview自适应高度
- 在 UITabBarController 和 UINavigationController tableView被navigati
- iOS TableView如何刷新指定的cell或section
- ios tableView那些事 (十三) 给 tableview 添加索引
- Java FX 8 - Tableview: Display Objects Within Objects
- 处理scrollview和tableview偏移64个像素的处理方法
- iOS - 一个页面多个tableview
- IOS下省市区选择器实现[基于TableView]
- 用TableView做的新闻客户端展示页面
- ios tableView那些事(四)tableView添加标题,头尾和改变cell的宽度---tableview
- qt Excel操作类和TableView继承和代理
- IOS8 设置TableView Separatorinset 分割线从边框顶端开始
- Swift_TableView(delegate,dataSource,prefetchDataSource 详解)
- dynamic-table-view-cell-height-auto-layout
- iOS tableview上拉加载更多时tableview跳动问题
- TableView的用法总结
- IOS 构造和使用TableView(基于storyboard) (一)基本的数据显示
- 设置 tableview 的背景颜色,总是不生效
- iOS边练边学--tableView的批量操作
- 解决tableview顶部留白一段的处理方法