众所周知,UITableViewCell默认情况下分隔线左侧会有一段缺失,当然我对于这种问题一般都忽略了~~~~(>_<)~~~~ ,如下图所示

但是有时候UI切图会要求分隔线占满屏幕宽度,如果你没有很好的办法解决,那么你得自定义一条分隔线了,对于一个“懒惰”的程序猿来说,太麻烦了(o)/~,然后你需要一种优雅的解决方式,那么在控制器里加上这段代码阔以完全满足的你的需求。
#pragma mark - 补全分隔线左侧缺失
- (void)viewDidLayoutSubviews {
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView 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];
}
}
运行结果如下图所示

已然和左侧无缝连接了,对比上图看着是不是酸爽多了?
网友评论