[英]Custom table cell not resizing to fit content view
我正在使用 3 种类型的自定义单元格实现表格视图。 如下所示,在公司信息部分,我的自定义表格视图单元格CompanyInfoTableViewCell
的总部标签被截断,因为我认为自定义表格单元格没有调整大小以适应内容视图。
我尝试在包含表视图的控制器的viewDidLoad
方法中实现以下代码。 这段代码假设让单元格自动调整大小以适应内容,但它不起作用(编辑:它不起作用,因为我正在使用无故事板编程地制作自定义单元格和表格视图)。 如何让我的自定义表格视图单元格显示我的所有内容?
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
CompanyInfoTableViewCell
代码:
class CompanyInfoTableViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
//Create industry label
industryLabel = UILabel(frame: CGRect(x: horOffset, y: 0, width: 0.8*screenSize.width, height: 21))
industryLabel.center.y = 15
contentView.addSubview(industryLabel)
//Create headquarters label
hqLabel = UILabel(frame: CGRect(x: horOffset, y: 0, width: 0.8*screenSize.width, height: 21))
hqLabel.center.y = 40
contentView.addSubview(hqLabel)
}
}
如果您没有使用故事板和 AutoLayout 约束,那么您需要根据UILabels
包含的内容手动计算其将采用的高度。 我没有看到任何根据内容计算高度并相应地设置标签高度以及自定义单元格高度的代码。
您必须实现heightForRowAtIndexPath
来设置单元格的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.