【问题标题】:Set table view height based on cell height for x visible cells根据 x 个可见单元格的单元格高度设置表格视图高度
【发布时间】:2017-07-04 14:44:01
【问题描述】:

我想调整我的表格视图高度,使其一次只显示 x 个单元格。单元格的布局是可配置的,所以我事先不知道它的高度。

为简单起见,我尝试设置一个单元格的最简单示例,该单元格有两个标签,固定文本垂直堆叠。

MyCustomViewCell.swift

class MyCustomViewCell: UITableViewCell {

    let label: UILabel
    let label2: UILabel

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        label = UILabel()
        label2 = UILabel()
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.contentView.addSubview(label)
        self.contentView.addSubview(label2)

        label.translatesAutoresizingMaskIntoConstraints = false

        label.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
        label.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true

        label2.translatesAutoresizingMaskIntoConstraints = false
        label2.topAnchor.constraint(equalTo: label.bottomAnchor).isActive = true
        label2.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
        label2.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
        label2.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true

        label.text = "string 1"
        label2.text = "string 2"
    }

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

通过在初始化表格视图时设置高度约束(优先级低于 1000),然后根据单元格的框架和单元格的数量在cellForRowAtIndexPath 上设置实际高度,我已经设法实现了所需的行为。想要可见(在本例中为 5)。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let cellIdentifier = "cellIdentifier"

    override func viewDidLoad() {
        super.viewDidLoad()

        let tableView = UITableView(frame: .zero, style: .plain)
        setupTableViewConstraints(tableView: tableView)

        tableView.register(MyCustomViewCell.self, forCellReuseIdentifier: cellIdentifier)
        tableView.dataSource = self
        tableView.delegate = self
    }

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

    func setupTableViewConstraints(tableView: UITableView) {
        self.view.addSubview(tableView)
        tableView.translatesAutoresizingMaskIntoConstraints = false

        tableView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
        let heightConstraint = tableView.heightAnchor.constraint(equalToConstant: 300)
        heightConstraint.priority = 900
        heightConstraint.isActive = true

        tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 10).isActive = true
        tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -10).isActive = true

        tableView.layer.borderColor = UIColor.black.cgColor
        tableView.layer.borderWidth = 0.5
    }

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

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

        tableView.heightAnchor.constraint(equalToConstant: cell.frame.height*5).isActive = true
        return cell
    }
}

但是,这种方法对我来说似乎不合适。从设置一个“虚拟”高度仅在以后调整到每次执行cellForRowAtIndexPath 时设置表格视图高度。

谁能给我指出一个更优雅的解决方案的方向?

【问题讨论】:

  • 你试过'tableView.tableFooterView = UIView(frame: CGRect.zero)'。您的表格视图实际上看起来像单元格的大小。

标签: ios swift uitableview autolayout


【解决方案1】:

在您的示例中,每次它要求您加载单元格时,您都会更新 tableview 的高度。

虽然这可行,但知道即使在单元格可见之前,表格视图也会不断调用此方法,这似乎有点矫枉过正。

为什么不等到 tableview 完成加载后才更新高度?

您可以在willDisplayCell 中执行此操作,而不是仅在单元格即将显示或可能像这样的完成关闭时触发:

self.tableView.reloadData()
DispatchQueue.main.async {
    // This closure is only called when the data has been reloaded
    // and thus will enable you to calculate the final content height
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 2012-04-22
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多