0

I have a viewcontroller which manages an uitableview with uicollectionviews in the uitableviewcells.

However, the uicollectionviews may contain varying numbers of items. Therefore I need to adjust the height of the uicollectionviews.

But when I try to do this the heights of the other uicollectionviews are also changed.

The uicollectionview is bound by an IBOutlet, as well as the constraint for the height of the collection view.

I'm setting the height of the collectionview in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath):

cell.collectionView.reloadData();
cell.collectionViewHeight.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height;
cell.collectionView.setNeedsLayout();
cell.collectionView.layoutIfNeeded(); 

First I'm reloading the data and then I'm setting the height constraint and finally I'm applying the new layout.

This seems to work at first - but I recognized that the other cells are also effected - probably because I'm dequeueing the reusable cell. But how can I change my code, such that it works?

Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • try this link : https://stackoverflow.com/questions/48093595/making-uitableview-with-embedded-uicollectionview-using-uitableviewautomaticdime/48211637#48211637 – Pallavi Srikhakollu Jan 17 '18 at 10:28
  • try this solution -> https://stackoverflow.com/questions/46724530/dynamic-collectionviewcell-in-tableviewcell-swift – dahiya_boy Jan 17 '18 at 10:29
  • Need your cell wire frame layout – SPatel Jan 17 '18 at 10:46
  • I'm curious, what is it that you're trying to create that requires to embed `UICollectionView` inside a `UITableViewCell` ? Maybe there are better approaches to achieve what you want :) – Laffen Jan 17 '18 at 12:00
  • Actually I want to have list items with a complex header and a following grid with some variable items. – Marcel Gangwisch Jan 17 '18 at 12:15
  • 1
    Have you tried using a custom supplementary view as a header in the `UICollectionView` ? – Laffen Jan 17 '18 at 12:36
  • No, I haven't tried that yet.. But I think this might be the solution.. Nesting uitableview and uicollectionview seems nasty. – Marcel Gangwisch Jan 17 '18 at 12:42

1 Answers1

1

You should implement 'tableView:heightForRowAtIndexPath:'

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    return 50.0
    // Or customize the way you want using the indexPath
}

or for UICollectionView 'collectionView:sizeForItemAtIndexPath:'

func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: widthPerItem, height: heightPerItem)
    }
GuiDupas
  • 1,681
  • 2
  • 22
  • 44
  • 1
    I need variable heights for the rows. And these heights depend on the height of the contained collectionview. – Marcel Gangwisch Jan 17 '18 at 12:19
  • Ok, I see. I have already done that updating the constraints of the content in cell and after that using 'cell.layoutIfNeeded(); ' before returning the cell. – GuiDupas Jan 17 '18 at 12:29