How to set UITableViewCell Height according to UICollectionView Content size?

1.8k views Asked by At

I have a UITableView and Inside that I have two UICollectionViews.

My requirement is that I have to scroll first UICollectionView Horizontally. And I have to scroll UITableView vertically Because I don't want to scroll Second UICollectionView.

In Image you can see that first (Header 'Folder') UICollectionView I have to scroll horizontally and second (Header 'Product') UICollectionView I don't want to scroll vertically because I have to scroll Complete UITableView.

Both collectionView are in TableViewCell and I have disable scrolling of second UICollectionView.

I have to add and delete a product then I how can I set the TableViewSecond Cell height that content should come.

enter image description here

enter image description here

Here is my code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if (0 == indexPath.section) {
        var addFolderCell = tableView.dequeueReusableCell(withIdentifier: "AddFolderCell") as? AddFolderCell

        if(nil == addFolderCell) {
            let nib:Array = Bundle.main.loadNibNamed("AddFolderCell", owner: self, options: nil)!
            addFolderCell = nib[0] as? AddFolderCell
        }

        addFolderCell?.collectionView.delegate = self
        addFolderCell?.collectionView.dataSource = self

        return addFolderCell!
    } else {
        let identifier = "CreateFolderCell"
        var createFolderCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? CreateFolderCell

        if(createFolderCell == nil) {
            let nib:Array = Bundle.main.loadNibNamed("CreateFolderCell", owner: self, options: nil)!
            createFolderCell = nib[0] as? CreateFolderCell
        }

        return createFolderCell!
    }
}

//Height for row.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //let index : NSIndexPath = NSIndexPath(row: 0, section: 1)
    if 0 == indexPath.section {
        return 135
    }
    return 300
}  

//Header height.

func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
    return 20
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sectionsArray[section] as? String
 }
}

extension GroupDataView : UICollectionViewDelegate,UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
      return 4
  }

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddFolderCollectionCell", for: indexPath) as! AddFolderCollectionCell

    //cell.backgroundColor = UIColor.white
    cell.layer.borderWidth = 0.4
    cell.layer.borderColor = UIColor.lightGray.cgColor
    cell.layer.cornerRadius = 2
    //CreateCardView(viewCorner: cell.cardView) //cell.cardView
    return  cell
  }
}  

//MARK: Collection View Layout

extension GroupDataView : UICollectionViewDelegateFlowLayout {

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     return CGSize(width: 90, height: 130)
  }
}

What can I do? How can I set the height for tableView?

1

There are 1 answers

0
Amr Hossam On

I think you can accomplish this, using some few steps:

  1. Subclass your UITableViewCell, let's name it CollectionCell, and change it's class in IB interface to new subclass name.

  2. Create outlet from UICollectionView to CollectionCell subclass, where UICollectionView is placed. Let's name it collectionView.

  3. Add constraints to all sides of collectionView to cell bounds. (top, right, bottom, left).

  4. Add in IB for collectionView height constraint and create outlet to CollectionCell too. Let's name it collectionViewHeight.

  5. In tableView:cellForRowAtIndexPath: add code:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // ...
        // normal cell declarations you wish
        // ...
    
        (cell as! CollectionCell).collectionView.reloadData()
        let height = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
        (cell as! CollectionCell).collectionViewHeight.constant = height
        return cell
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100 // <- Your Desired Height
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableViewAutomaticDimension
    }