gpt4 book ai didi

ios - 如何计算动态类型的 estimatedRowHeight

转载 作者:行者123 更新时间:2023-11-28 12:34:06 24 4
gpt4 key购买 nike

我有一个 UITableViewController 的自定义子类。它有一个包含许多行的部分。每行对应于相同的自定义表格 View 单元格类。每个自定义单元格都有两个标签:myLabel1myLabel2,它们都是单元格的 contentView 的 subview 。

每个 myLabel1 都有一行文本,每个 myLabel2 都有一两行文本,但是每个单元格应该有相同的高度,就好像每个 >myLabel2 有两行文本。

标签使用动态类型。

myLabel1.font = UIFont.preferredFont(forTextStyle: .headline)
myLabel2.font = UIFont.preferredFont(forTextStyle: .subheadline)

根据 Working with Self-Sizing Table View Cells ,我用自动布局定位了每个标签,并“将 TableView 的 rowHeight 属性设置为 UITableViewAutomaticDimension”,以便行高随动态类型变化。

  1. 如何让每个单元格具有相同的高度?

  2. 我应该如何估计表格 View 的行高?

最佳答案

UITableViewAutomaticDimension 将根据单元格内容大小估计单元格大小。我们必须计算一个单元格可能具有的最大高度,并为所有单元格返回此高度,而不是使用 UITableViewAutomaticDimension

CustomTableViewCell

let kMargin: CGFloat = 8.0

class CustomTableViewCell: UITableViewCell {

@IBOutlet var singleLineLabel: UILabel!
@IBOutlet var doubleLineLabel: UILabel!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
updateFonts()
}

override func prepareForReuse() {
updateFonts()
}

func updateFonts() {
singleLineLabel.font = UIFont.preferredFont(forTextStyle:.title3)
doubleLineLabel.font = UIFont.preferredFont(forTextStyle:.body)
}

func updateCellForCellWidth(_ width:CGFloat) {
doubleLineLabel.preferredMaxLayoutWidth = width - (2*kMargin)
}

func fillCellWith(_ firstString: String, _ secondString: String) {
singleLineLabel.text = firstString
doubleLineLabel.text = secondString
}
}

在 View Controller 上

设置虚拟单元格并列出动态类型的通知

var heightCalculatorDummyCell: CustomTableViewCell!
var maxHeight: CGFloat = 0.0

override func viewDidLoad() {
super.viewDidLoad()
heightCalculatorDummyCell = tableView.dequeueReusableCell(withIdentifier: "cell_id") as! CustomTableViewCell
maxHeight = getMaxHeight()

NotificationCenter.default.addObserver(self, selector: #selector(AutomaticHeightTableViewController.didChangePreferredContentSize), name: .UIContentSizeCategoryDidChange, object:nil)
}

使用虚拟单元格获取最大高度。

func getMaxHeight() -> CGFloat {
heightCalculatorDummyCell.updateFonts()
heightCalculatorDummyCell.fillCellWith("Title","A string that needs more than two lines. A string that needs more than two lines. A string that needs more than two lines. A string that needs more than two lines. A string that needs more than two lines.")
heightCalculatorDummyCell.updateCellForCellWidth(tableView.frame.size.width)
let size = heightCalculatorDummyCell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
return (size.height + 1)
}

根据通知处理表格 View 重新加载

deinit {
NotificationCenter.default.removeObserver(self)
}

func didChangePreferredContentSize() {
maxHeight = getMaxHeight()
tableView.reloadData()
}

最后一步,在 tableview delegate 中返回最大高度

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

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return maxHeight
}

enter image description here

关于ios - 如何计算动态类型的 estimatedRowHeight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41356282/

24 4 0
文章推荐: CSS
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com