在多种编辑模式下调整UITableViewCell分隔线

时间:2016-09-20 19:04:32

标签: ios swift uitableview

我想在iOS 9/10的标准UITableView中以多种编辑模式调整分隔符插入边距。

我的目标是将分隔线与单元格文本标签的左边缘对齐

在过去,我完全删除了边距:

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或绘制线条?

2 个答案:

答案 0 :(得分: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;
} 

答案 1 :(得分: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
}

它也有效,不知道为什么它不适合你...