UITableViewCell分隔线补全方法

在使用tableView的时候发现, cell分割线右边有一点缺失.总结三个补全方法,使cell的分割线补全:
1 添加一个自动以view

实现步骤:

  • 取消系统的分隔线
  • 自定义cell, 在layOutsubviews添加自定义分隔线
#import "ViewController.h"
#import "JNCell.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 取消系统的分隔线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pargma mark --自定义cell
#import "JNCell.h"
@implementation JNell

- (void)layoutSubviews {

    [super layoutSubviews];

    // 初始化数据
    CGFloat w = self.frame.size.width;
    CGFloat h = 1;
    CGFloat x = 0;
    CGFloat y = self.frame.size.height - 1;
    // 添加自定义分隔线
    UIView *sepLine = [[UIView alloc] init];

    sepLine.frame = CGRectMake(x, y, w, h);

    // 设置背景色
    sepLine.backgroundColor = [UIColor colorWithRed:150/255.0 green:150/255.0  blue:150/255.0  alpha:1];

    [self.contentView addSubview:sepLine];
}
@end
2 在TableView中所在的方法中添加相应的方法

注意 :
该方法在iOS7.0系统及以下会报错;因为layoutMargins属性是8.0才有的

- (void)viewDidLayoutSubviews {

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        self.tableView.layoutMargins = UIEdgeInsetsZero;
    }
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        self.tableView.separatorInset = UIEdgeInsetsZero;
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        cell.layoutMargins = UIEdgeInsetsZero;
    }

    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        cell.separatorInset = UIEdgeInsetsZero;
    }
}
3 通过背景颜色偷梁换柱

实现方法:

  • 取消系统的分隔线
  • 设置tableView的背景颜色
  • 自定义cell, 重写系统的setFrame:方法
#import "ViewController.h"
#import "JNCell.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 1.0 取消系统的分隔线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    // 2.0 设置背景颜色
    self.tableView.backgroundColor = [UIColor grayColor];
}

#pargram mark -- 自定义cell
#import "JNCell.h"
@implementation ABCell

// 重写系统方法
- (void)setFrame:(CGRect)frame {
    // 设置分隔线的高度
    frame.size.height -= 1;
    // 调用系统的方法
    [super setFrame:frame];
}

/**
当系统自动计算好cell的尺寸后, 会调用该方法给cell设置frame

这时候重写setframe方法, 把cell的高度减1; 故显示后会露出高度为1(tableView的)背景颜色 (充当分隔线)
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 从iOS7.0开始, tableViewCell的分隔线总会在左侧留意下一点小间距, 这是苹果更新系统之后闹出的幺...
    面糊阅读 5,039评论 0 50
  • 1.创建一个UITableView对象 ITableView *tableView = [[UITableView...
    桐生一猫阅读 5,283评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,094评论 19 139
  • 废话不多说,直接上干货 ---------------------------------------------...
    小小赵纸农阅读 8,821评论 0 15
  • 一禅冷月吴钩,如来几多愁? 低眉垂眸,大千世界莲出污垢。 也曾痴怨也曾修,累世千劫情难够, 谁又能载得动这许多寂寥秋……
    易郁生阅读 3,437评论 2 2

友情链接更多精彩内容