如下初始化可以解决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之后必须加入此代码才可以解决。