Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic UICollectionView inside dynamic UITableViewCell

I have a collection view that is dynamic and can have any number of cells in it. It is positioned inside a table view cell.

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

My table view is correctly configured to be dynamic because the other cells work fine so the issue is not there.

Here is what I currently have... As you can see there is no collection view below

enter image description here

This is what my desired out come is... (Forcing a height constraint on the collection view)

enter image description here

I have configured constraints on all sides of the collection view correctly, as it works when I give a fix height constraint. But this defeats the object of a dynamic collection view...

I have linked an outlet to the UICollectionViewFlowLayout and set an estimated cell size and given the cell the correct constraints on the label inside as you can see in the image where I forced the height constraint.

like image 667
Niall Kiddle Avatar asked Apr 19 '18 22:04

Niall Kiddle


People also ask

What is the difference between UICollectionView and UiTableView?

UICollectionView can do pretty much everything that UiTableView can do. The collection view is much more robust in terms of layout features and other things like animations. The way to set up both views with cell registration, dequeuing the cells, specifying size and heights are pretty much the same.

How do I make a collection view dynamic height?

Make-CollectionView-Height-Dynamic-According-to-ContentAdd a height constraint to your collection view. Set its priority to 999. Set its constant to any value that only makes it reasonably visible on the storyboard. Change the bottom equal constraint of the collection view to greater or equal.

How do you optimize scrolling performance of dynamically sized table or collection views?

Use Self-Sizing Cells for Cells of Variable Height In case the cells we want to display in our table view have variable height, we can use self sizable cells. Basically, we should create appropriate Auto Layout constraints to make sure the UI components that have variable height will stretch correctly.


1 Answers

Create a subclass for collectionView and override intrinsicContentSize.

class DynamicCollectionView: UICollectionView {
  override func layoutSubviews() {
    super.layoutSubviews()
    if !__CGSizeEqualToSize(bounds.size, self.intrinsicContentSize) {
        self.invalidateIntrinsicContentSize()
     }
  }

   override var intrinsicContentSize: CGSize {
    return collectionViewLayout.collectionViewContentSize
   }
}
  1. In Interface builder change the class of your collectionView to DynamicCollectionView (subclass UICollectionView).

  2. Set estimated cell size of UICollectionViewFlowLayout.

        flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
    
like image 124
Learner Avatar answered Oct 20 '22 16:10

Learner