1. 程式人生 > >UITableView分割線左邊空白解決方法

UITableView分割線左邊空白解決方法

ios7中,左側會有預設15畫素的空白。設定setSeparatorInset:UIEdgeInsetsZero 能將空白去掉。

ios8中,setSeparatorInset:UIEdgeInsetsZero 的設定已經不起作用了。

下面是解決方法:

首先在viewDidLoad方法加入以下程式碼:

 [self.tableView setSeparatorColor:[UIColor colorWithRed:(227/255.0) green:(229/255.0) blue:(231/255.0) alpha:0.9]];

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 *)indexPath

{

    if ([cell respondsToSelector:@selector(setSeparatorInset:)])

    {

        [cell setSeparatorInset:UIEdgeInsetsZero];

    }

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

    {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

}