如何计算内部具有stackview的自定义单元格的大小?

时间:2018-05-09 08:02:28

标签: ios swift uitableview

我需要制作自定义UiTableview单元格。我已经做到了,它按预期工作得很好。但是现在在一个单元格中,我必须显示多个记录(取决于学生注册的主题的数量)。所以我可以像这样简化我的问题

案例 我有一份来自服务器的学生名单,其中列出了每个学生的入学名单。所以我必须向每个学生及其多个注册科目展示。

我的方法 我创建了一个带有UiTextLabel的单元格来显示名称。在下面我已经垂直拍摄了一个Stackview,它将显示主题的数量。因此,我将以编程方式将每个学生注册的科目填入此垂直堆栈视图中。

代码

 func createEnrollemtStackView(_ mEnrollment : EnrollmentServiceModel ) -> UIStackView {
    var enrolledIn = "Not enrolled"

    if(mEnrollment.GradeServiceModel != nil && mEnrollment.GradeServiceModel?.Name != nil && mEnrollment.GradeServiceModel?.Name != ""){
        let gradeName = (mEnrollment.GradeServiceModel?.Name)!
        let gradeSectionName = mEnrollment.GradeSectionName
        let batchName = mEnrollment.BatchServiceModel?.Name

        enrolledIn = "\(gradeName) - \(gradeSectionName!) (\(batchName))"

    }else{

        let batchName = mEnrollment.BatchServiceModel?.Name
        let className = mEnrollment.ClassServiceModel?.Name

        enrolledIn = "\(batchName) (\(className)) school"
    }


    let lblEnrolledIn = UILabel()
    lblEnrolledIn.text = "Enrolled in"
    lblEnrolledIn.textColor = UIColor(hex:"F48024")
    lblEnrolledIn.font = lblEnrolledIn.font.withSize(10)
    lblEnrolledIn.font = UIFont.boldSystemFont(ofSize: 10.0)

    let lblEnrolledStatus = UILabel()
    lblEnrolledStatus.text = "Enrolled Status"
    lblEnrolledStatus.textColor = UIColor(hex:"F48024")
    lblEnrolledStatus.font = lblEnrolledStatus.font.withSize(10)
    lblEnrolledStatus.font = UIFont.boldSystemFont(ofSize: 10.0)

    let tvEnrolledIn = UILabel()
    tvEnrolledIn.text = enrolledIn
    tvEnrolledIn.font = tvEnrolledIn.font.withSize(13)
    tvEnrolledIn.backgroundColor = UIColor.green

    let tvEnrolledStatus = UILabel()
    tvEnrolledStatus.text = mEnrollment.EnrollmentStaus
    tvEnrolledStatus.font = tvEnrolledStatus.font.withSize(13)

    let stackViewHorizontal   = UIStackView()
    stackViewHorizontal.axis  = UILayoutConstraintAxis.horizontal
    stackViewHorizontal.distribution  = UIStackViewDistribution.fillEqually
    stackViewHorizontal.alignment = UIStackViewAlignment.leading
    stackViewHorizontal.spacing = 5


    let enrollmentSV = UIStackView()
    enrollmentSV.axis  = UILayoutConstraintAxis.vertical
    enrollmentSV.distribution  = UIStackViewDistribution.fillEqually
    enrollmentSV.alignment = UIStackViewAlignment.leading
    enrollmentSV.spacing = 3
    enrollmentSV.backgroundColor = UIColor.brown


    enrollmentSV.addArrangedSubview(lblEnrolledIn)
    enrollmentSV.addArrangedSubview(tvEnrolledIn)


    enrollmentSV.translatesAutoresizingMaskIntoConstraints = false
    enrollmentSV.leadingAnchor.constraint(equalTo: stackViewHorizontal.leadingAnchor,constant:0)
    enrollmentSV.trailingAnchor.constraint(equalTo: stackViewHorizontal.trailingAnchor,constant:0)
    enrollmentSV.topAnchor.constraint(equalTo: stackViewHorizontal.topAnchor,constant:0)
    enrollmentSV.bottomAnchor.constraint(equalTo: stackViewHorizontal.bottomAnchor,constant:0)



    let statusSV = UIStackView()

    statusSV.axis  = UILayoutConstraintAxis.vertical
    statusSV.distribution  = UIStackViewDistribution.fillEqually
    statusSV.alignment = UIStackViewAlignment.leading
    statusSV.spacing = 3
    statusSV.backgroundColor = UIColor.blue
    //
    //
    statusSV.addArrangedSubview(lblEnrolledStatus)
    statusSV.addArrangedSubview(tvEnrolledStatus)



    statusSV.translatesAutoresizingMaskIntoConstraints = false
    statusSV.leadingAnchor.constraint(equalTo: stackViewHorizontal.leadingAnchor,constant:0)
    statusSV.trailingAnchor.constraint(equalTo: stackViewHorizontal.trailingAnchor,constant:0)
    statusSV.topAnchor.constraint(equalTo: stackViewHorizontal.topAnchor,constant:0)
    statusSV.bottomAnchor.constraint(equalTo: stackViewHorizontal.bottomAnchor,constant:0)



    stackViewHorizontal.addArrangedSubview(enrollmentSV)
    stackViewHorizontal.addArrangedSubview(statusSV)



    return stackViewHorizontal
}

我从以下方法调用此函数

  

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath) - > UITableViewCell {

这个函数给了我2个水平对齐的stackview,它们本身是verticall,包含我的注册数据,如注册和注册状态。

数据有效。但我面临着问题。这些都是如下。

  1. 如何计算行高??
  2. 为什么我在设计的堆栈视图中以编程方式添加的stackview具有左边的边距?

1 个答案:

答案 0 :(得分:1)

您在enrollmentSV中添加statusSVstackViewHorizontal的代码中存在错误:

    stackViewHorizontal.addArrangedSubview(enrollmentSV)
    stackViewHorizontal.addArrangedSubview(statusSV)

因此无需在enrollmentSV& statusSV。它们将使用stackViewHorizontal自动调整。

对于行高,您不需要计算大小。虽然只需将高度UITabelViewAutomaticDimension指定为:

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

但为此您应该在细胞上附加顶部和底部约束。请按照下图:

堆叠是您的stackViewHorizontal,其顶部,底部,前导和尾随附加到UITableViewCell

由于顶部和底部约束以及UITableViewAutomaticDimension,这些表格单元格将变为自行调整大小

enter image description here

更新

如果您的堆栈在视图中,在单元格内。不要指定高度以仅查看top, bottom, leading and trailing w.r.t.到ContentView,它将自动从内部堆栈的内容中获取:

enter image description here