UICollectionView inside TableView not populating cells?

I have a tableView with rows that should be horizontally current collectionViews.

In my tableView cells there is one CollectionView inside them, and then I create them as follows:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("MyTableViewCell", forIndexPath: indexPath) as! MyTableViewCell let nibName = UINib(nibName: "CollectionCell", bundle: nil) cell.collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CollectionCell") cell.collectionView.dataSource = self cell.collectionView.delegate = self return cell } 

I am trying to use a table view row of height-height, and I think this might cause problems. How to use UITableViewAutomaticDimension with internal collection views?

+7
ios uitableview swift uicollectionview
source share
2 answers

This is in objective-c, but works for me:

I. In your custom .m cell element, register the UICollectionViewCell register and specify the layout details (size, spacing, etc.).

II. in a VC that contains a tableView, its .m does the following in cellForRowAtIndexPath

  MyTableViewCell *cell = (MyTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"MyTableViewCellId" forIndexPath:indexPath]; cell.collectionView.delegate = self; cell.collectionView.dataSource = self; cell.collectionView.tag = indexPath.row; [cell.collectionView reloadData]; 

III. You can use the UICollectionView tag in delegate methods to fill in the required information.

This will help.

+7
source share

Try adding - cell.setTranslatesAutoresizingMaskIntoConstraints (false) - see below

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: MyTableViewCell = tableView.dequeueReusableCellWithIdentifier("MyTableViewCell", forIndexPath: indexPath) as! MyTableViewCell let nibName = UINib(nibName: "CollectionCell", bundle: nil) cell.collectionView.registerNib(nibName, forCellWithReuseIdentifier: "CollectionCell") cell.collectionView.dataSource = self cell.collectionView.delegate = self cell.setTranslatesAutoresizingMaskIntoConstraints(false) return cell } 

For more information see https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells .

0
source share

All Articles