开发笔记 - 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 ,strongUITableView *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 设置分割线颜色:

_mainTableView.separatorColor = [UIColor redColor];//分割线颜色

2.特殊场景应用:

单元格想实现以下效果: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下完成的,如有问题,请评论告知,多谢!




0 0