簡體   English   中英

根據x個可見單元格的單元格高度設置表格視圖高度

[英]Set table view height based on cell height for x visible cells

我想調整表格視圖的高度,以便一次只顯示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仍會不斷調用此方法,這似乎有點過頭了。

為什么不等到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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM