Search code examples
iosxcodeuitableviewuicollectionviewswift4

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

  • Here is the code how I done for same inside TableViewCell using collectionView and performed selection :

    Code inside TableView datasource method :

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCarColorCell", for: indexPath) as! BasicCarColorCell
        cell.dataSource = preloads.colors
        cell.collectionViewSetup()
        cell.delegate = self
        return cell
    }
    

    Code for TableViewCell:

    protocol BasicCarColorCellDelegate {
    func colorCell(cell:BasicCarColorCell, didSelect color: Color)
    }
    
    class BasicCarColorCell: UITableViewCell {
    var dataSource = Array<Color>()
    var selectedColor = Color()
    
    @IBOutlet weak var textView: RSKPlaceholderTextView!
    
    @IBOutlet weak var guideLineMessage:UILabel!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    var delegate: BasicCarColorCellDelegate?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    func collectionViewSetup() {
        let nib = UINib(nibName: "BasicCarColorCollectionCell", bundle: nil)
        self.collectionView.register(nib, forCellWithReuseIdentifier: "BasicCarColorCollectionCell")
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.scrollDirection = .horizontal
        collectionView.collectionViewLayout = flowLayout
        collectionView.dataSource = self
        collectionView.delegate = self
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
    }
    
    }
    
    
    extension BasicCarColorCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BasicCarColorCollectionCell", for: indexPath) as! BasicCarColorCollectionCell
    
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let height = collectionView.bounds.size.height-2
        let width = height-20
        return CGSize(width: width, height:height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 1, left: 10, bottom: 1, right: 10)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let color = self.dataSource[indexPath.item]
        self.selectedColor = color
        delegate?.colorCell(cell: self, didSelect: self.selectedColor)
        collectionView.reloadData()
    }
    
    }
    

    And to handle the selection of collectionView just implement the method of custom protocol written in TableViewCell in ViewController:

    func colorCell(cell: BasicCarColorCell, didSelect color: Color) {
        //self.selectedCarColor = color.value
    
    }
    

    You can do it in same pattern as per your need.

    Hope it'll help!