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:

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

Inside tableviewcell is scrolling I do not want the inside tableview cell to scroll differently.
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:
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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With