如何在Swift中调整TableView标题的缩进?

时间:2017-03-31 04:36:09

标签: ios swift uitableview tableview

我知道如何设置标题文本并设置样式,但对于我的生活,我找不到任何解释如何调整文本缩进的内容。这就是我试过的:

for i in range(len(primes) - 1):
    if primes[i] - primes[i + 1] == g:
        r.append(primes[i - 1])
        r.append(primes[i])
result.append(r[0])
result.append(r[1])

上面的代码(以及我尝试的每个变体)都会导致错误:

  

无法激活约束,因为它们没有共同的祖先。

这导致我对我的电脑说,“如果func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){ let header = view as! UITableViewHeaderFooterView header.textLabel?.translatesAutoresizingMaskIntoConstraints = false header.textLabel?.leftAnchor.constraint(equalTo: header.leftAnchor, constant: 15).isActive = true } 让我到header.textLabelUILabel确实是header,我估计你应该知道一个人是另一个人的祖先!“

1 个答案:

答案 0 :(得分:5)

您可以首先为整个separatorInset设置TableView,如下所示:

let headerInset: CGFloat = 50
tableView.separatorInset = UIEdgeInsets.init(top: 0, left: headerInset, bottom: 0, right: 0)

然后,如果您想将表格的单元格放置在TableView边缘的阵容中,您可以使用forRowAt indexPath方法执行此操作:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    cell.separatorInset = UIEdgeInsets.zero

}
相关问题