UITableView分割线问题

在实际开发中由于UI设计我们很少会使用系统自带的分割线,之前都是隐藏系统分割线(_tabbleView.separatorStyle = UITableViewCellSeparatorStyleNone;)然后自己在cell中添加一个的View设置背景色与高度进而达到效果分割线的效果。当我们需要的是两端到顶的分割线时,可以直接修改系统的分割线。当然如果需要的并不是两端到顶的我们只要重新设置UIEdgeInsets * set = UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right);替换UIEdgeInsetsZero以达到所要实现的效果。

设置两端间距为0的代码⬇️
// 设置分割线颜色
_groupsTableView.separatorColor = [UIColor colorWithHexString:@"2c2c2d"];
#pragma mark - 设置分割线的方法
-(void)viewDidLayoutSubviews {
    if ([_groupsTableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_groupsTableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([_groupsTableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [_groupsTableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容