Here is a quick solution. you need to configure the collection data source and delegate it to the tableview cell using the tableView delegation method cellForRowAtIndexPath, then use the data collection method and delegation method in viewController, where you validated the data source and the tableveiew delegate. Here is the code for mainViewController:
extension MainViewController:UITableViewDataSource, UITableViewDelegate { // .....do some table view setup like numberOfRowsInSection ...... func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("yourReusecellIdentifier", forIndexPath: indexPath) as! yourCustomCell cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) return cell } }
Here, the code cell.setcollectionViewDataSourceDelegate (self, forRow: indexPath.rom) sets the collection delegate and data source to the table view cell, then drag the collection output to the tableview cell. In the tableView cell, add mehod setcollectionViewDataSourceDelegate to set the delegate of the View collection as follows:
class yourCustomCell: UITableViewCell {
After that, use the delegate method of viewing the collection as a controller:
extension MainViewController: UICollectionViewDelegate, UICollectionViewDataSource { func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 2 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 5 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewReuseIdentifier", forIndexPath: indexPath) as! YourCollectionViewCustomCell .......cell configure.... return cell } } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { print("get selected collectionview itemindex \(indexPath.row)") } }
For a clear explanation, visit https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/
bikram sapkota
source share