简体   繁体   English

如何在 swift 的 maintableviewcell 中访问内部 tableviewcell 的高度

[英]How can I access Height of inside tableviewcell in the maintableviewcell in swift

I am new in swift and I am unable to access the height of inside UITableviewCell in main UITableviewCell.我是 swift 的新手,我无法访问主 UITableviewCell 中 UITableviewCell 内部的高度。

in my main tableviewcell my code in cellforrowatindexpath my code is like this在我的主 tableviewcell 我在 cellforrowatindexpath 中的代码我的代码是这样的

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

I created the height constraint of inside tableview and I am multiplying it based on the count of array.我创建了 tableview 内部的高度约束,并根据数组的计数将其相乘。 it works.有用。 But if the data in cell increase it is not getting the height proper because I define here static height.但是,如果单元格中的数据增加,则无法获得正确的高度,因为我在这里定义了 static 高度。 So I want the inside tableviewcell height so it could work proper.所以我想要内部的 tableviewcell 高度,这样它才能正常工作。 can I access it.我可以访问它吗? Please help Thanks in Advance!请帮助提前致谢!

I want to acchive this我想实现这个

在此处输入图像描述

But I am getting this as the content of insidetableviewcell is increased但是随着 insidetableviewcell 的内容增加,我得到了这个

在此处输入图像描述

Inside tableviewcell is scrolling I do not want the inside tableview cell to scroll differently.内部 tableviewcell 正在滚动我不希望内部 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 上的高度约束来实现的。

Steps if your tableView is inside Storyboard or xib:如果您的 tableView 在 Storyboard 或 xib 内,请执行以下步骤:

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

If your tableView is created programmatically, make sure its a child of SelfSizedTableView.如果您的 tableView 是以编程方式创建的,请确保它是 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()
}

} }

You can try using below delegate method to provide the height for each row in your tableView:您可以尝试使用下面的委托方法来提供 tableView 中每一行的高度:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   // calculate the height and return.
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

}

Also try un-ckecking Automatic check box for "Row Height" and "Estimate" in the Size Inspector of Tableview as below:还可以尝试在 Tableview 的 Size Inspector 中取消选中“Row Height”和“Estimate”的 Automatic 复选框,如下所示: 在此处输入图像描述

You can take IBOutlet of inside Tableview and get height from bounds like insideTableView.bounds.height您可以使用 Tableview 内部的 IBOutlet 并从像 insideTableView.bounds.height 这样的边界获取高度

If you need the cell height of the main tableview then you can try to get like mainTableView.rowHeight or mainTableView.estimatedRowHeight如果您需要主 tableview 的单元格高度,那么您可以尝试获取 mainTableView.rowHeight 或 mainTableView.estimatedRowHeight

If you don't want the inside Table view scroll then you can disable scrolling by unchecking Scrolling Enabled in Attribute Inspector如果您不希望内部表格视图滚动,那么您可以通过取消选中 Attribute Inspector 中的 Scrolling Enabled 来禁用滚动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM