Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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. It works. But if the data in cell increase it is not getting the height proper because I define here static height.

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

I want to achieve this:

enter image description here

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

enter image description here

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

like image 289
Muju Avatar asked Dec 05 '25 04:12

Muju


1 Answers

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

Previously that was achieved using height constraint on tableView.

Steps if your tableView is inside Storyboard or xib:

  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.

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()
}

}

like image 175
Muhammad Waqas Avatar answered Dec 07 '25 18:12

Muhammad Waqas