2016-09-20 34 views
3

我正在尋找在iOS 9/10中的標準UITableView中的多個編輯模式下調整分隔符插入邊距。在多編輯模式下調整UITableViewCell分隔線

我的目標是使與細胞的文本標籤的左邊緣的分隔線。

在過去,我已經完全做刪除的邊距:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    cell.separatorInset = .zero 
    cell.layoutMargins = .zero 
} 

這給了我一個觀點,如:

如果我嘗試要增加分隔符插入,整個文本標籤移過來,這是我不想要的。 :

cell.separatorInset = UIEdgeInsetsMake(0, 40, 0, 0) 

如何我只是移動分隔線,在不影響文本佈局,最好沒有做的layoutSubviews任何超越或手動畫線?

+0

你有沒有想通這一個? –

+0

@StianHøiland您是否曾嘗試上述問題中的代碼?因爲它在我身邊工作。 – JackyW

回答

1

您可以手動添加分隔符,嘗試使用下面的代碼

func viewDidLoad() { 
    self.tableview.separatorStyle = UITableViewCellSeparatorStyle.None 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    let separator = CALayer() 

    separator.backgroundColor = UIColor.grayColor(); 
    separator.frame = CGRectMake(0, 43, self.view.frame.size.width, 1); 
    cell.layer.insertSublayer(separator, atIndex:0) 
    return cell; 
} 
+0

這將工作,但正如我所說我想避免手動繪製線條。特別是在這種情況下,不能保證複選標記的寬度是固定的,因此後續更新可能會破壞此方法。 – chrismanderson

+0

是的,但設置插入也不能保證,我認爲如果複選框的寬度在將來更新中發生變化,您必須相應地更改分隔幀的插入。 –

0

我在Xcode中8,與iOS 9

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
    cell.layoutMargins = .zero 
} 

給你你要根據你的第三張照片是什麼。

我已經試過

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
{ 
    cell.layoutMargins = .zero 
} 

它也能工作,不知道爲什麼它不工作就在你身邊..

相關問題