Preeti Rani
Preeti Rani

Reputation: 695

Need to use Collectionview inside tableviewCell with dynamic height of collectionviewcell in swift

Although I found so much questions already asked here related to this question, but still not able to get what I wanted to.

My Question is very much similar to this question asked by @Rui, I Want a Layout similar to Zomato

Let me explain all with the help of this example :

1) I used a tableview as a superContainerView so that i can put any number of blocks like "Pocket-friendly delivery".

2) Then I used Collectionview inside one tableviewcell for making its inside-views which is dynamic , it can be 3 or 6 or 9.

3)Inside CollectionViewCell I designed that custom view which contains a set of image and label (i.e Cafe Green Apple like View ) as seen below.

4)I have assigned tableview as well as collectionview delegate and datasource to same UIViewController.

5) Everything is Working fine but when i assign 6 to numberOfItemsInCollectionView , the size of collectionView remains same and it started Scrolling , which i don't want.

I want all 6 to be visible without any need of scrolling.

enter image description here

Please anyone help me on this.

Upvotes: 2

Views: 1854

Answers (1)

user3608500
user3608500

Reputation: 835

Add height constraint for collection view inside cell and calculate the height of collection view based on the number of images,assuming the image size is constant. Set the collection view height constraint. Do all of these in tableView cellForRowAtIndexPath method of your table view. For your tableview still you can use UITableViewAutomaticDimension.

Upvotes: 1

Related Questions