设置tableView的分割线从最左端开始

苹果原生的tableView的cell分割线默认是没有从最左端开始,有时候这样不免影响美观,有时也会有这样的需求,设置tableView的分割线从最左端开始:

不多说了,直接上代码吧:

//分割线从顶端开始  --------适配iOS7 8----------
- (void)viewDidLayoutSubviews{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}
- (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];
    }
}

  效果前后对比图:

原文地址:https://www.cnblogs.com/h-tao/p/5223134.html