文章标题
2015-12-27 14:38
375 查看
UITableView
自带滑动效果
创建方法和UIView相同
创建
UITable有两套协议方法,需要签两个协议
主要协议方法
注:1.2.3用的比较多
1.主要功能就是实现点击
2.通过这个方法可以让tableView显示内容
3.设置tableView里有多少个分区
4.设置不同行高
5.指定每个分区有多少行
6.设置分区的标题
自带滑动效果
创建方法和UIView相同
创建
UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; tableView.backgroundColor=[UIColor yellowColor]; [self.view addSubview:tableView];
UITable有两套协议方法,需要签两个协议
@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>
//设置行高 tableView.rowHeight=100; //设置代理人 tableView.dataSource=self; tableView.delegate=self;
主要协议方法
注:1.2.3用的比较多
1.主要功能就是实现点击
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"row = %ld",indexPath.row); NSLog(@"section = %ld",indexPath.section); NSLog(@"%@",self.arr[indexPath.row]); }
2.通过这个方法可以让tableView显示内容
//这个方法只要有cell要出现,就会触发 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //tableViewCell通过重用避免了多余的创建,一般来讲一个tableView现实的cell数是有限的,所以为了提高效率,避免重复的创建,利用重用解决问题重用也是常见的tableView面试问题 //1.现指定一个cell的重用标识 //一般来讲,一个tableview对应一个重用标识,重用标志作用就是告诉系统哪个cell对应哪个tableView static NSString *reuse=@"reuse"; //系统先会根据重用标识在重用池里找,有没有用闲置的cell,如果有直接拿来用,如果没有,再创建 UITableViewCell *cell=[tableView dequeueReusableHeaderFooterViewWithIdentifier:reuse]; //如果没找到对应的cell是0x0 if (!cell) { cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease]; } //cell提供了三种视图,两个label,一个imageView cell.textLabel.text=self.arr[indexPath.row]; cell.detailTextLabel.text=[NSString stringWithFormat:@"%ld",indexPath.section]; cell.imageView.image=[UIImage imageNamed:@"c4.jpg"]; // NSLog(@"%ld",indexPath.row); return cell; }
3.设置tableView里有多少个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3; }
4.设置不同行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row %2 ==0) { return 100; }else{ return 50; } }
5.指定每个分区有多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if(section%2==0){ return 10; }else{ return self.arr.count; } }
6.设置分区的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ return [NSString stringWithFormat:@"%ld",section]; }
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ return @"hello"; }
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return @[@"a",@"b",@"c"]; }
相关文章推荐
- LaTeX格式模板二
- Oracle Alert - APP-ALR-04108: SQL error ORA-01455
- [英语学习]国外的在线广播网站,类似喜马拉雅和荔枝FM
- Programming Language Grammar Comparsion
- NSString,NSArray相关用法
- JavaScript 运动框架
- Masonry的个人理解
- 【HTML第二本】HTML.5与CSS.3权威指南 CSS3部分读书笔记
- C语言学习笔记------伊能C语言学习笔记-----外部函数
- 用C#实现通用的DAL Insert
- 冒泡排序
- [LeetCode]228.Summary Ranges
- OpenCV的安装与系统环境变量
- 【动态规划】矩阵链乘法
- 也谈指针类型的区分
- 机器学习十大经典算法支持向量机SVM(Support Vector Machine)(上篇)
- Gradle学习笔记(三)安装Gradle及Hello World
- 地道的表达
- Java算法---华为oj查找组成一个偶数最接近的两个素数
- 5.zabbix学习笔记:zabbix深入配置和使用 告警配置(一)