'CollectionView delegate methods are not calling from TableViewCell

Screenshot

Delegate isn't being called from tableViewCell. Here is UICollectionViewDelegate and UICollectionViewDataSource code

extension HomeVC:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProductCell", for: indexPath)
        return cell

    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("ok")

    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lay = collectionViewLayout as! UICollectionViewFlowLayout
        lay.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
        lay.minimumInteritemSpacing = 0
        lay.minimumLineSpacing = 0
        collectionView.collectionViewLayout = lay
        let size = (collectionView.frame.size.width-10) / 2
        return CGSize(width: size, height: size)
    }

}


Solution 1:[1]

swift 5

//MARK:- Tableview Datasource method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier:"RecentLoanApplicationCell") as! RecentLoanApplicationCell
                           
                cell.collectionVC.delegate = self
                cell.collectionVC.dataSource = self
                cell.btnViewAll.addTarget(self, action: #selector(btnViewALLTap(button:)), for: .touchUpInside)
                DispatchQueue.main.async {
                               
                                cell.collectionVC.reloadData()
                 }
                return cell  
}

//MARK:- Tableview Cell in side custom cell Register for collectionView cell

class RecentLoanApplicationCell: UITableViewCell {

@IBOutlet  var collectionVC:UICollectionView!

override func awakeFromNib() {
    super.awakeFromNib()

    self.collectionVC.register(UINib.init(nibName:"RecentLoanCollectionViewCell", bundle: nil), forCellWithReuseIdentifier:"RecentLoanCollectionViewCell")
    
    
    
    }
override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

}

}

// Same ControllerView  call  
//MARK:- Collection DataSource & Delegate

extension HomeViewController : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
            return 5   
    }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {  
    
let cell = collectionView.dequeueReusableCell(withReuseIdentifier:"RecentLoanCollectionViewCell", for: indexPath) as! RecentLoanCollectionViewCell
        
         return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)    {

        debugPrint("didSelectItemAt ==>\(indexPath.row)")
}

Solution 2:[2]

You need to set datasource and delegate for collectionView, every time in Tableview delegate CellForRowAtIndexPath.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shubham bairagi