【问题标题】:How to create custom cells 100% programmatically in Swift?如何在 Swift 中以编程方式 100% 创建自定义单元格?
【发布时间】:2020-02-08 00:06:48
【问题描述】:

我正在尝试以编程方式构建 TableView,但我无法获得要显示的基本标准标签;我所看到的只是基本的空单元格。这是我的代码:

表格视图单元格:

class TableCell: UITableViewCell {

    let cellView: UIView = {
       let view = UIView()
        view.backgroundColor = .systemRed
        return view
    }()

    let labelView: UILabel = {
        let label = UILabel()
        label.text = "Cell 1"
        return label
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setup()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setup() {
        addSubview(cellView)
        NSLayoutConstraint.activate([
            cellView.topAnchor.constraint(equalTo: topAnchor),
            cellView.bottomAnchor.constraint(equalTo: bottomAnchor),
            cellView.leadingAnchor.constraint(equalTo: leadingAnchor),
            cellView.trailingAnchor.constraint(equalTo: trailingAnchor)])
        cellView.addSubview(labelView)
    }
}

数据来源:

class TableDataSource: NSObject, UITableViewDataSource {
    let cellID = "cell"

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! TableCell
        return cell
    }
}

这就是 VC:

class TableViewController: UITableViewController {

    let dataSource = TableDataSource()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(TableCell.self, forCellReuseIdentifier: dataSource.cellID)
        tableView.dataSource = dataSource
    }
}

我试图使代码尽可能基本以供将来参考。我已经设置了各种断点来查看可能出现的问题,但它们都检查出来了。会不会是约束有问题?

感谢任何帮助。

【问题讨论】:

  • 您应该将视图添加到contentView,而不是直接添加到单元格。您的标签没有任何限制。理想情况下使用backgroundView,而不是用红色子视图覆盖整个单元格。

标签: ios swift uitableview user-interface


【解决方案1】:

我在您的单元格中发现了几个错误。

  1. 将子视图添加到contentView,而不是直接添加到单元格:

    contentView.addSubview(cellView)
    cellView.addSubview(labelView)
    

    约束也是如此:

    NSLayoutConstraint.activate([
        cellView.topAnchor.constraint(equalTo: contentView.topAnchor),
        cellView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        cellView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        cellView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
    ])
    
  2. 代码中创建的视图需要设置translatesAutoresizingMaskIntoConstraints = false

    let cellView: UIView = {
       let view = UIView()
        view.backgroundColor = .systemRed
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()
    
    let labelView: UILabel = {
        let label = UILabel()
        label.text = "Cell 1"
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
    
    1. 您的标签没有任何限制。

【讨论】:

  • 谢谢!我不知道contentView。像魅力一样工作。
  • @fankibiber contentView 的原因是单元格包含其他视图,例如backgroundViewselectedBackgroundView、操作按钮(“删除”)等
【解决方案2】:

您的约束不起作用,因为您需要在setup() 中将translatesAutoresizingMaskIntoConstraints 更改为cellView

    func setup() {
        addSubview(cellView)
        cellView.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            cellView.topAnchor.constraint(equalTo: topAnchor),
            cellView.bottomAnchor.constraint(equalTo: bottomAnchor),
            cellView.leadingAnchor.constraint(equalTo: leadingAnchor),
            cellView.trailingAnchor.constraint(equalTo: trailingAnchor)])
        cellView.addSubview(labelView)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多