解决TableViewCell分割线默认左边间隔15点

来源:互联网 发布:小白编程网站 编辑:程序博客网 时间:2024/04/30 00:01


如下初始化可以解决cell默认左边间距问题

_tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    //_tableView.backgroundColor = [UIColor darkGrayColor];

    _tableView.delegate = self;

    _tableView.dataSource = self;

    _tableView.separatorInset = UIEdgeInsetsZero;

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

        _tableView.layoutMargins = UIEdgeInsetsZero;

    }

    [self.view addSubview:_tableView];

然后在UITableView的代理方法中加入以下代码

- (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];

}

}


备注:

1._tableView.separatorInset = UIEdgeInsetsZero; iOS8之前可以直接用这个就可以解决。

2.

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

        _tableView.layoutMargins = UIEdgeInsetsZero;

    } IOS8之后必须加入此代码才可以解决。

0 0
原创粉丝点击