代码改变世界

UITableView分割线设置

2016-01-11 14:42  蓝梦儿  阅读(628)  评论(0编辑  收藏  举报

公司UI偏爱分割线顶格,有觉得系统的颜色不好,所以敲代码的时候还得各种处理分割线,总有想掐死谁的冲动,记录一下,还是修改系统的~

设置tableView,在viewDidLoad中写这个

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

还要写这个代理方法

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
   if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

可以在这个方法里设置每个cell的分割线左边间隔,来应对UI不同的需求

设置分割线的颜色

_mTableView.separatorColor = Fenge_Color;