【发布时间】:2019-11-23 02:36:22
【问题描述】:
我有以下代码来定义我的自定义 tableView 单元格类:
import UIKit
import ChameleonFramework
class IsectionsCustomTableViewCell: UITableViewCell {
let sectionDesignationLabelTopPadding: CGFloat = 10
let sectionDesignationLabelLeftPadding: CGFloat = 10
let sectionDesignationLabelRightPadding: CGFloat = 10
let depthOfSectionLabelTopPadding: CGFloat = 5
let depthOfSectionLabelLeftPadding: CGFloat = 10
let depthOfSectionLabelRightPadding: CGFloat = 2.50
let webThicknessLabelTopPadding: CGFloat = 5
let webThicknessLabelLeftPadding: CGFloat = 2.50
let webThicknessLabelRightPadding: CGFloat = 10
let widthOfSectionLabelTopPadding: CGFloat = 5
let widthOfSectionLabelLeftPadding: CGFloat = 10
let widthOfSectionLabelRightPadding: CGFloat = 2.50
let sectionFlangeThicknessLabelTopPadding: CGFloat = 5
let sectionFlangeThicknessLabelLeftPadding: CGFloat = 2.5
let sectionFlangeThicknessLabelRightPadding: CGFloat = 10
let sectionMassPerMetreLabelTopPadding: CGFloat = 5
let sectionMassPerMetreLabelLeftPadding: CGFloat = 10
let sectionMassPerMetreLabelRightPadding: CGFloat = 2.5
var tableCellContainer: UIView = {
let container = UIView()
container.translatesAutoresizingMaskIntoConstraints = false
container.backgroundColor = UIColor.flatPink()
return container
}()
var sectionDesignationLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.backgroundColor = UIColor.yellow
label.textAlignment = .left
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor(hexString: "#F27E63")
return label
}()
var depthOfSectionLabel: UILabel = {
let label = UILabel()
label.backgroundColor = UIColor.blue
label.numberOfLines = 0
label.textAlignment = .left
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor(hexString: "#F27E63")
return label
}()
var widthOfSectionLabel: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.textAlignment = .left
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor(hexString: "#F27E63")
label.backgroundColor = UIColor.gray
return label
}()
var sectionWebThicknessLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor(hexString: "#F27E63")
label.backgroundColor = UIColor.cyan
label.textAlignment = .left
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byWordWrapping
return label
}()
var sectionFlangeThicknessLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor(hexString: "#F27E63")
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.numberOfLines = 0
label.textAlignment = .left
label.backgroundColor = UIColor.green
return label
}()
var sectionMassPerMetreLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = UIColor(hexString: "#F27E63")
label.lineBreakMode = NSLineBreakMode.byWordWrapping
label.numberOfLines = 0
label.textAlignment = .left
label.backgroundColor = UIColor.white
return label
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(tableCellContainer)
tableCellContainer.addSubview(sectionDesignationLabel)
tableCellContainer.addSubview(depthOfSectionLabel)
tableCellContainer.addSubview(sectionWebThicknessLabel)
tableCellContainer.addSubview(widthOfSectionLabel)
tableCellContainer.addSubview(sectionFlangeThicknessLabel)
tableCellContainer.addSubview(sectionMassPerMetreLabel)
applyAppropriateSizeAndConstraintsForCellItems()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func applyAppropriateSizeAndConstraintsForCellItems() {
NSLayoutConstraint.activate([
tableCellContainer.topAnchor.constraint(equalTo: self.topAnchor),
tableCellContainer.leftAnchor.constraint(equalTo: self.leftAnchor),
tableCellContainer.rightAnchor.constraint(equalTo: self.rightAnchor),
tableCellContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor),
sectionDesignationLabel.topAnchor.constraint(equalTo: tableCellContainer.topAnchor, constant: sectionDesignationLabelTopPadding),
sectionDesignationLabel.leftAnchor.constraint(equalTo: tableCellContainer.leftAnchor, constant: sectionDesignationLabelLeftPadding),
sectionDesignationLabel.rightAnchor.constraint(equalTo: tableCellContainer.rightAnchor, constant: -1*sectionDesignationLabelRightPadding),
depthOfSectionLabel.topAnchor.constraint(equalTo: sectionDesignationLabel.bottomAnchor, constant: depthOfSectionLabelTopPadding),
depthOfSectionLabel.leftAnchor.constraint(equalTo: tableCellContainer.leftAnchor, constant: depthOfSectionLabelLeftPadding),
depthOfSectionLabel.rightAnchor.constraint(equalTo: tableCellContainer.superview!.centerXAnchor, constant: -1*depthOfSectionLabelRightPadding),
sectionWebThicknessLabel.topAnchor.constraint(equalTo: sectionDesignationLabel.bottomAnchor, constant: webThicknessLabelTopPadding),
sectionWebThicknessLabel.leftAnchor.constraint(equalTo: tableCellContainer.superview!.centerXAnchor, constant: webThicknessLabelLeftPadding),
sectionWebThicknessLabel.rightAnchor.constraint(equalTo: tableCellContainer.rightAnchor, constant: -1*webThicknessLabelRightPadding),
widthOfSectionLabel.topAnchor.constraint(equalTo: depthOfSectionLabel.bottomAnchor, constant: widthOfSectionLabelTopPadding),
widthOfSectionLabel.leftAnchor.constraint(equalTo: tableCellContainer.leftAnchor, constant: widthOfSectionLabelLeftPadding),
widthOfSectionLabel.rightAnchor.constraint(equalTo: tableCellContainer.superview!.centerXAnchor, constant: -1*widthOfSectionLabelRightPadding),
sectionFlangeThicknessLabel.topAnchor.constraint(equalTo: sectionWebThicknessLabel.bottomAnchor, constant: sectionFlangeThicknessLabelTopPadding),
sectionFlangeThicknessLabel.leftAnchor.constraint(equalTo: tableCellContainer.superview!.centerXAnchor, constant: sectionFlangeThicknessLabelLeftPadding),
sectionFlangeThicknessLabel.rightAnchor.constraint(equalTo: tableCellContainer.rightAnchor, constant: -1*sectionFlangeThicknessLabelRightPadding),
sectionMassPerMetreLabel.topAnchor.constraint(equalTo: widthOfSectionLabel.bottomAnchor, constant: sectionMassPerMetreLabelTopPadding),
sectionMassPerMetreLabel.leftAnchor.constraint(equalTo: tableCellContainer.leftAnchor, constant: sectionMassPerMetreLabelLeftPadding),
sectionMassPerMetreLabel.rightAnchor.constraint(equalTo: tableCellContainer.superview!.centerXAnchor, constant: -1*sectionMassPerMetreLabelRightPadding)
])
}
}
当我为所有大于 150 的单元格指定高度时,上述方法效果很好(请参阅附图)。
但是,我想要实现的是自定义 TableViewCell 类内部代码中定义的 tableCellContainer UIView 能够根据其中的子视图计算其高度。因此,我根据附加的图像,我希望能够计算sectionDesignationLabelTopPadding + + sectionDesignationLabelHeight + depthOfSectionLabelTopPadding + depthOfSectionLabelHeight + widthOfSectionLabelTopPadding + widthOfSectionLabelHeight + massPerMetreLabelTopPadding + massPerMetreLabelHeight 的高度。然后将总数与 sectionDesignationLabelTopPadding + sectionDesignationLabelHeight + webThicknessLabelTopPadding + webThicknessLabelHeight +flangeThicknessLabelTopPadding +flangeThicknessLabelHeight 的总数进行比较。我希望将两者之间的最大总和设置为 tableCellContainer UIView 的高度。
我尝试做的如下:
在包含tableView的ViewController内部,特别是cellForRowAt indexPath函数内部,计算每个标签的高度,然后将最大总和设置为容器的高度。但是,这不起作用,因为它为每个 UILabel 项的高度返回了错误的值。
有人可以指导我如何实现我想要的东西的最佳位置和方法吗,非常感谢?
问候, 沙迪。
【问题讨论】:
标签: swift uitableview dynamic height