以编程方式动态计算自定义TableView Cell的高度?

时间:2019-07-13 18:56:04

标签: swift uitableview dynamic height

我有以下代码来定义我的自定义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的单元格指定高度时,上述方法效果很好(请参阅所附图像)。 enter image description here

但是,我想实现的是在自定义TableViewCell类内的代码中定义的tableCellContainer UIView能够基于其中的子视图计算其高度。因此,基于附件图像,我希望能够计算sectionDesignationLabelTopPadding +的高度+ sectionDesignationLabelHeight + depthOfSectionLabelTopPadding + depthOfSectionLabelHeight + widthOfSectionLabelTopPadding + widthOfSectionLabelHeight + massPerMetreLabelTopPadding + massPerMetreLabelHeight。然后,将总数与sectionDesignationLabelTopPadding + sectionDesignationLabelHeight + webThicknessLabelTopPadding + webThicknessLabelHeight + leadbThicknessLabelTopPadding + flangeThicknessLabelHeight的总数进行比较。我希望将两者之间的最大合计设置为tableCellContainer UIView的高度。

我试图做的如下:

在包含tableView的ViewController内部,特别是在cellForRowAt indexPath函数内部,以计算每个标签的高度,然后将最大总计设置为容器的高度。但是,这不起作用,因为它为每个UILabel项目的高度返回了错误的值。

请多谢高级人员,有人可以指导我找到最佳方法和最佳方法,以实现所需的东西吗?

关于, 沙迪。

1 个答案:

答案 0 :(得分:0)

首先,这是要共享的EPIC大小代码。您应该始终从较小的代码开始,以测试要学习的新知识。

第二,您要查找的功能称为自定义tableView单元格。有很多教程可以帮助您实现这一目标。

但我很快向您解释:

  • 首先,所有子视图应具有自动调整大小的能力(例如UILabelUIButton或您拥有自定义类),或具有constraint s的精确高度大小。

  • 然后它们全部都应以某种方式与单元格contentView(或单元格本身)相关。像这样:V: contentViewTop-subview1-subview2-subview3-contentViewBottom

  • 然后,如果将tableView.rowHeight设置为UITableView.automaticDimension,它将自动获取所有子视图的固有内容大小并将其应用于单元格高度。

本教程可以帮助您更好地了解更多细节:

Self-sizing-table-view-cells