0



I have one UITableView, inside UITableViewCell thire is oneUICollectionView (Horizontal scrolling).

UICollectionViewCell will expand on click as per content in label, this logic is done by using numberOfLine property set to 2 or 0 of UILable inside UICollectionViewCell.

UICollectionView is getting expand perfectly but UITableViewCell height is not expanding as per content height of UICollectionView.

I check this answer

UICollectionView inside a UITableViewCell -- dynamic height?

I can expand UICollectionView by using this but its not helping me to expand UITableViewCell

I also try systemLayoutSizeFittingSize method but not working out,

below is the code:

//ViewController

 class ViewController: UIViewController {

    //MARK: -Outlet Declaration-
    @IBOutlet weak var tblView: UITableView!

    //MARK: -Variable Declaration-

    //MARK: -ViewController Life Cycle Methods-

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //MARK: -Button Methods-
}

extension ViewController : UITableViewDelegate ,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1    
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellTableView", for: indexPath) as? CellTableView

        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
    }


}

//TableViewCell

class CellTableView: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!

    //MARK: -Variable Declaration-
    var isExpandCell = false

    override func awakeFromNib() {
        super.awakeFromNib()
        setCollectionViewFlowLayout()
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    func setCollectionViewFlowLayout()  {
        let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = UICollectionViewFlowLayout.automaticSize
        flowLayout.estimatedItemSize = CGSize.init(width:UIScreen.main.bounds.width, height: 1)
    }

    // THIS IS THE MOST IMPORTANT METHOD
    //
    // This method tells the auto layout
    // You cannot calculate the collectionView content size in any other place,
    // because you run into race condition issues.
    // NOTE: Works for iOS 8 or later

//    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
//        self.collectionView.frame = CGRect.init(x: 0, y: 0, width: targetSize.width, height:targetSize.height)
//        return self.collectionView.collectionViewLayout.collectionViewContentSize
//    }
}
extension CellTableView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CellCollectionView", for: indexPath) as? CellCollectionView
        cell?.lblDynamicContent.numberOfLines = (isExpandCell ? 0  : 2)
        cell?.lblDynamicContent.text = Constant.kDummyText
        return cell!
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        isExpandCell = !isExpandCell

        collectionView.reloadData()
    }

}

// ColletionView Cell

class CellCollectionView: UICollectionViewCell {

    //MARK: -Outlet Declaration-
    @IBOutlet weak var lblDynamicContent: UILabel!


    //MARK- -Override Methods-
    override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        setNeedsLayout()
        layoutIfNeeded()
        let size = systemLayoutSizeFitting(layoutAttributes.size)
        var frame = layoutAttributes.frame
        frame.size.height = ceil(size.height)
        layoutAttributes.frame = frame
        return layoutAttributes
    }
}
  • Can you share the complete project for me to take a look? A github repo should do. – Saurabh Yadav Apr 12 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.