我们自己创建的tableView中默认cell左边会有一段空白,下面的方法可以清除空白,注意:这是tableView的代理方法,需要先遵循tableView的代理协议
OC的写法
//清除cell左边线的空白
- (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];
}
}
Swift3.0的写法
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter:tableView.separatorInset)) {
//UIEdgeInsetsZero
tableView.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter:tableView.layoutMargins)) {
tableView.layoutMargins = UIEdgeInsets.zero
}
}