【问题标题】:Indent for a title in a table缩进表格中的标题
【发布时间】:2020-05-20 05:22:20
【问题描述】:

我有一张有两个部分的桌子。标题部分的属性textAligment = .right。但它离手机的边框太近了。如何从靠近中心的边框缩进?

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {

        let header = view as! UITableViewHeaderFooterView

        header.textLabel?.textColor = .black
        header.textLabel?.textAlignment = .right

    }

【问题讨论】:

  • 你可以尝试继承UILabel并覆盖drawTextInRect:
  • 不要使用默认类,子类UITableViewHeaderFooterView 并使用您想要的定位/对齐方式添加标签。

标签: ios swift uitableview header swift5


【解决方案1】:

这是一个非常简单的例子……

  • 在情节提要中添加UITableViewController
  • 将其自定义类分配给SectionTableViewController(来自下面的代码)。
  • 使用默认单元格。
  • 给单元格一个标识符“DefCell”

运行应用程序。这是你应该看到的:

我已将标签限制为使用 contentView.layoutMarginsGuide 并将尾随常量设置为 -32

class MySectionHeaderView: UITableViewHeaderFooterView {

    let myLabel: UILabel = {
        let v = UILabel()

        v.translatesAutoresizingMaskIntoConstraints = false
        v.textColor = .black
        v.textAlignment = .right

        // during development, give it a background color to make it easy to see the frame
        //v.backgroundColor = .cyan

        return v
    }()

    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        commonInit()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    func commonInit() -> Void {

        contentView.addSubview(myLabel)

        // use layout margins guide
        let g = contentView.layoutMarginsGuide

        NSLayoutConstraint.activate([
            myLabel.topAnchor.constraint(equalTo: g.topAnchor, constant: 0.0),
            myLabel.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: 0.0),
            myLabel.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 0.0),

            // set the constant to provide more trailing space as desired
            // Note: must be a negative value
            myLabel.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -32.0),
        ])

    }

}

class SectionTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.register(MySectionHeaderView.self, forHeaderFooterViewReuseIdentifier: "MyHeader")
    }

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 10
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let c = tableView.dequeueReusableCell(withIdentifier: "DefCell", for: indexPath)
        c.textLabel?.text = "Section: \(indexPath.section) Row: \(indexPath.row)"
        return c
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let v = tableView.dequeueReusableHeaderFooterView(withIdentifier: "MyHeader") as? MySectionHeaderView else {
            fatalError("Could not dequeue MySectionHeaderView!")
        }
        v.myLabel.text = "Section \(section)"
        return v
    }

}

您会在MySectionHeaderView 中的myLabel 声明中注意到这些行:

    // during development, give it a background color to make it easy to see the frame
    //v.backgroundColor = .cyan

这是标签具有背景色时的外观,因此您可以轻松查看标签的布局方式:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多