iOS应用开发中UITableView的分割线的一些设置技巧


对于ios7,ios8及以上来说,调整UITableView的cell的分割线位置已经是相当不便,因为UITableView内部使用了margin layout.

其实只需要如下这样子就可以实现分割线的控制。

复制代码 代码如下:

-(void)tableView:(UITableView )tableView willDisplayCell:(UITableViewCell )cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 下面这几行代码是用来设置cell的上下行线的位置
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
    [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    //按照作者最后的意思还要加上下面这一段,才能做到底部线控制位置,所以这里按stackflow上的做法添加上吧。
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

如果要直接使用TableView的sectionTitle,但又想设置它的字体,颜色什么的,可以使用如下方法。

复制代码 代码如下:

- (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor blueColor];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor redColor]];

// 另一种方法设置背景颜色
// header.contentView.backgroundColor = [UIColor blackColor];
}

不显示分割线
通过tableFooterView修改UITableView分割线:
在使用UITableView的时候,如果没有数据/数据很少,会发现即使没有数据的cell也会有分割线,这样看起来并不美观,通常我们希望只有显示数据的cell会显示对应的分割线,而不显示数据的cell不显示分割线。
常用的做法有两种:

第一种做法是首先取消显示分割线,然后自定义cell,在cell的最底部加上一个高度为1的view,这样看起来就像是一条分割线。只有cell有数据显示出来的时候才会显示这个view,这样就达到了目的。

第二种做法既不用取消显示分割线,也不需要自定义cell,而是直接这样做:

复制代码 代码如下:

self.tableView.tableFooterView = [[UIView alloc] init];

运行显示结果,发现就已经达到了我们的目的。很明显这种做法更方便。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3