如何在Swift的maintableviewcell中访问内部tableviewcell的高度 [英] How can I access Height of inside tableviewcell in the maintableviewcell in Swift

查看:24
本文介绍了如何在Swift的maintableviewcell中访问内部tableviewcell的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 新手,无法访问主 UITableviewCell 中 UITableviewCell 内部的高度.

I am new in Swift and I am unable to access the height of inside UITableviewCell in main UITableviewCell.

在我的主 tableviewcell 中,cellforrowatindexpath 中的代码如下所示:

In my main tableviewcell my code in cellforrowatindexpath my code is like this:

cell.constraintHeightOfTblView.constant = CGFloat(135 * (cell.arrCourses.count))

我创建了内部 tableview 的高度约束,并根据数组的数量乘以它.有用.但是如果单元格中的数据增加,它就不会得到正确的高度,因为我在这里定义了静态高度.

I created the height constraint of inside tableview and I am multiplying it based on the count of array. It works. But if the data in cell increase it is not getting the height proper because I define here static height.

所以我想要内部 tableviewcell 高度,以便它可以正常工作.我可以访问它吗?

So I want the inside tableviewcell height so it could work proper. Can I access it?

我想实现这一目标:

但是随着 insidetableviewcell 的内容增加,我得到了这个

But I am getting this as the content of insidetableviewcell is increased

内部 tableviewcell 正在滚动我不希望内部 tableview 单元格以不同的方式滚动.

Inside tableviewcell is scrolling I do not want the inside tableview cell to scroll differently.

推荐答案

目的 这段代码的目的是为 tableView 提供适合其内容大小的高度并停止滚动.

Purpose of this code is to provide a tableView a height that fits its content size and stop scrolling.

以前是使用 tableView 上的高度约束来实现的.

Previously that was achieved using height constraint on tableView.

如果您的 tableView 在 Storyboard 或 xib 中的步骤:

Steps if your tableView is inside Storyboard or xib:

  1. 创建一个名为 SelfSizedTableView 的 swift 文件.
  2. 提供 SelfSizedTableView 到 Storyboard 或 xib 中的 tableView.
  3. 无需为 tableView 提供高度约束.
  4. 只需转到 Size Inspector 转到底部并设置 Intrinsic大小到占位符,不勾选高度和宽度框.
  1. Create a swift file named SelfSizedTableView.
  2. Provide SelfSizedTableView to your tableView that is inside Storyboard or xib.
  3. No need to provide height constraint to tableView.
  4. Just go to Size Inspector go to bottom and set Intrinsic Size to placeholder, without checking boxes of height and width.

如果您的 tableView 是以编程方式创建的,请确保它是 SelfSizedTableView 的子项.

If your tableView is created programmatically, make sure its a child of SelfSizedTableView.

import UIKit

class SelfSizedTableView: UITableView {

/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
    // Drawing code
}
*/

func defaultSetup() {
    // do something here
}

override open func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    defaultSetup()
}

override init(frame: CGRect, style: UITableView.Style) {
    super.init(frame: frame, style: style)
    defaultSetup()
}

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    defaultSetup()
}

/// set a value to maxHeight if you want tableView to scroll after a specific height
/// e.g. when contentSize.height > maxHeight
/// table view will start to scroll

var maxHeight: CGFloat?

/// set a value to minHeight if you want tableView to avoid scroll before specific height
/// e.g. when contentSize.height > minHeight
/// table view will start to scroll

var minHeight: CGFloat?

override func reloadData() {
    super.reloadData()
    self.invalidateIntrinsicContentSize()
    self.layoutIfNeeded()
}

override var intrinsicContentSize: CGSize {
    let height: CGFloat
    
    if let maxHeight = maxHeight {
        height = min(contentSize.height, maxHeight)
    }
    else if let minHeight = minHeight {
        height = max(contentSize.height, minHeight)
    }
    else {
        height = contentSize.height
    }
    
    return CGSize(width: contentSize.width, height: height)
}

override func layoutSubviews() {
    super.layoutSubviews()
    invalidateIntrinsicContentSize()
}

}

这篇关于如何在Swift的maintableviewcell中访问内部tableviewcell的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆