开发笔记 - UITableView相关
来源:互联网 发布:灰熊猫知乎 编辑:程序博客网 时间:2024/06/09 16:18
1.1常用到的宏定义
#define SC_WIDTH [UIScreen mainScreen].bounds.size.width#define SC_HEIGHT [UIScreen mainScreen].bounds.size.height
1.2初始化
@property (nonatomic ,strong) UITableView *mainTableView;
<pre name="code" class="objc">-(void)addMainTableView{ if (_mainTableView == nil) { _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SC_WIDTH, SC_HEIGHT - 64) style:UITableViewStyleGrouped]; _mainTableView.delegate = self; _mainTableView.dataSource = self; _mainTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); _mainTableView.rowHeight = 45; [self.view addSubview:_mainTableView]; }}
1.3常用数据源方法
#pragma -mark tabView 数据源方法//每个分区的单元格数量-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 4;}//区头高度-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 0.0001;}//区尾高度-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return SC_HEIGHT - 64 - 45 * 4;}//设置单元格-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; } cell.textLabel.font = [UIFont systemFontOfSize:15]; return cell;}#pragma -mark cell点击方法-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [tableView deselectRowAtIndexPath:indexPath animated:YES];}
1.4常用设置
1.4.1 cell显示箭头:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
1.4.2 cell分割线左对齐设置或者取消分割线
_mainTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置分割线左对齐_mainTableView.separatorStyle =UITableViewCellSeparatorStyleNone;//取消分割线
cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell选择态
1.4.3 设置分割线颜色:
2.特殊场景应用:_mainTableView.separatorColor = [UIColor redColor];//分割线颜色
单元格想实现以下效果:section的分割线不显示,只显示cell的分割线
解决方案一:
初始化TableView时候风格为:UITableViewStylePlain,即:
_mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SC_WIDTH, SC_HEIGHT - 64) style:UITableViewStylePlain];这是最简单的一个方法,在SDK底层已经封装好了这种样式,为什么Plain这种风格在我们实际开发中很少用到?大概是因为这种风格的TableView的区头和区尾会常驻屏幕顶端/底部,除非是特殊需求是很少会用到的,但是这种风格的TableView的section分割线是没有的,此时需要解决的问题是让区头和区尾跟随滑动,而不是常驻,解决方法是在ScrollView的代理方法中手动设置TableView的偏移量,代码如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat sectionHeaderHeight = 10;//你设置的区头的高度 if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0); }}但是这样写会出现一个Bug,就是在TableView向上滑动之后整个TableView会向上偏移你设置的区头的高度,如下图所示:解决方案二:
初始化TableView时候风格为:UITableViewStylePlain,即:
<pre name="code" class="objc">_mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SC_WIDTH, SC_HEIGHT - 64) style:UITableViewStyleGrouped];这种情况下就只能自定义cell,或者是在cell的方法里面添加一下代码:
UIView *view = [cell.contentView viewWithTag:1889]; if (view == nil) { UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SC_WIDTH, 0.5)]; lineView.backgroundColor = RGB_COLOR(200, 199, 204); lineView.tag = 1889; [cell.contentView addSubview:lineView]; } if (indexPath.row == 0) { view.alpha = 0; } else { view.alpha = 1; }
此种方法需要设置TableView的分割线风格为None_mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;总结:如果在区头,或者区尾,或者有多个分区时区头/区尾高度不固定的情况下,建议使用
UITableViewStyleGrouped这种风格,然后自定义分割线,虽说多写几句代码,但是更加稳定,也不会存在一些安全隐患。
注:除此之外有些大牛说可以把区头和区尾设置为一个只初始化的View,代码如下:
_mainTableView.tableFooterView = [[UIView alloc] init];可是本人试了一下,没什么效果,可能是跟编译器有关,本人此次的测试是在Xcode 8.0下完成的,如有问题,请评论告知,多谢!
- 开发笔记 - UITableView相关
- 开发学习笔记--UItableView
- iOS开发学习笔记 UITableview
- iOS学习笔记--03 UITableView相关
- UITableView相关
- UITableView相关
- UITableView相关
- UITableView相关
- UITableView相关
- iOS开发学习笔记——UITableview
- iOS开发笔记--UITableView入门
- iOS开发笔记之UITableView的使用
- IOS开发笔记30-UITableView(1)
- IOS开发笔记31-UITableView(2)
- IOS开发笔记32-UITableView(3)
- IOS开发笔记33-UITableView(1)
- iOS开发笔记-UITableView的单选
- IOS开发笔记-UITableView的多选
- Hive窗口和分析函数[RANK()、DENSE_RANK()、ROW_NUMBER()]
- openwrt 问题六 搭建自己的交叉编译环境
- Unity For Android 打包时的Texture Compression选项
- 远程连接MySql问题
- 某联接口由控件对象模式转为结构过程模式的操作
- 开发笔记 - UITableView相关
- Python Web 开发学习 - 学习笔记(2)- 启动Python
- API性能测试基本性能指标及要求
- 第一站-部署开发工具
- hadoop1.2.1的安装
- erlang beam文件反编译
- 转载的开源干货
- Java基础--数组
- Windows环境下使用python selenium 打开Firefox的问题总结