Muju
Muju

Reputation: 979

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.

Upvotes: 3

Views: 1092

Answers (2)

Muhammad Waqas
Muhammad Waqas

Reputation: 900

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

}

Upvotes: 3

Nandish
Nandish

Reputation: 1177

You can try using below delegate method to provide the height for each row in your 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: enter image description here

You can take IBOutlet of inside Tableview and get height from bounds like 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

If you don't want the inside Table view scroll then you can disable scrolling by unchecking Scrolling Enabled in Attribute Inspector

Upvotes: 2

Related Questions