swift - 我的 TableViewCell 不会根据它内部的动态变化视图来改变它的高度
问题描述
我已经尝试对 heightForRowAt 和estimatedHeightForRowAt 使用 UITableView.automaticDimension ,并且单元格的结果相同,不会随着动态变化的视图而变化。
以下是一些相关代码:
从视图控制器:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// return getSize(large: 70, medium: 60, small: 50)
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
从 tableview 单元格:
func setup(_ lineItem: MenuItem) {
contentView.backgroundColor = .white
configureTransactionView(lineItem)
configureItemNameLabel(lineItem)
configurePriceLabel(lineItem)
configureSizeLabel(lineItem)
}
private func configureTransactionView(_ lineItem: MenuItem) {
itemView = UIView()
guard let itemView = itemView else { return }
itemView.clipsToBounds = true
itemView.layer.cornerRadius = 5
itemView.layer.borderColor = UIColor.lightGray.cgColor
itemView.layer.borderWidth = 0.5
itemView.backgroundColor = .white
contentView.addSubview(itemView)
itemView.translatesAutoresizingMaskIntoConstraints = false
itemView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
itemView.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: 0.8).isActive = true
itemView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 4).isActive = true
let fontSize = getSize(large: 14, medium: 13.5, small: 12)
guard let topFont = UIFont(name: AppFont.secondary.name, size: fontSize) else { return }
let heightForTopLabel = heightForView(text: lineItem.name, font: topFont, width: 195)
var text = ""
if lineItem.quantity > 1 && lineItem.toppings != nil {
text = generateTextWithMultipleSelected(lineItem)
} else if lineItem.quantity > 1 {
if lineItem.size != nil {
guard let size = lineItem.size else { return }
text = "\(size) | Quantity: \(lineItem.quantity)"
} else {
text = "Quantity: \(lineItem.quantity)"
}
} else if lineItem.toppings != nil {
text = generateTextWithSingleSelected(lineItem)
} else {
if lineItem.size != nil {
guard let size = lineItem.size else { return }
text = "\(size)"
} else {
text = ""
}
}
let bottomFontSize = getSize(large: 11, medium: 10.5, small: 9.5)
guard let font = UIFont(name: AppFont.secondary.name, size: bottomFontSize) else { return }
let heightForBottomLabel = heightForView(text: text, font: font, width: 200)
let height = heightForTopLabel + heightForBottomLabel + 10
itemView.heightAnchor.constraint(equalToConstant: height).isActive = true
}
解决方案
在您的 TableViewController viewDidLoad() 添加以下内容:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 600 //change according to your needs
同时删除 heightForRowAtIndexPath 和 esttimetedHeightForRowAtIndexPath 方法。
这是一个很好的教程: https ://www.raywenderlich.com/8549-self-sizing-table-view-cells
推荐阅读
- android - 在 RecyclerView 中加载更多问题
- android - 用于在两个 EditTexts 之间匹配文本内容的 Android Espresso 测试
- android - Firebase 用户配置文件中的配置文件图片未显示在真实设备上
- android - 从 C++ 访问 Android Settings.Secure
- java - Phaser 相位值转义
- android - 可绘制缩小不适用于另一个模块中声明的资产
- ruby-on-rails - 如何从钱包模型中获取 simple_form 上的电子邮件成员?【复杂关系】
- c++ - c++字符串常量和静态初始化顺序惨败
- c# - 如果 materialId 和 depotId 列相同,我想总结我的 IncomingProduct 表的金额列。最后我想减去这些。MVC C#
- amazon-web-services - 通过使用 Terraform 提供 AWS 区域,在现有 VPC 中预置 EC2