swifteveryday2020
swifteveryday2020

Reputation: 25

How to get UITableView row when user taps on UICollectionViewCell

App Scheme-

Image

As you can see from my App Scheme I have UITableView and inside UITableViewCells I have UICollectionView.

My question right now is how to get UITableView row when user taps on UICollectionViewCell?

I tried this but that doesn't work me:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)

    let main = ViewController()
    if let tableViewcell = superview?.superview as? ExploreTableViewCell {
        let indexPath = main.tableView.indexPath(for: tableViewcell)
        
        
        print(indexPath)
    }
}

Upvotes: 2

Views: 79

Answers (2)

StonedStudio
StonedStudio

Reputation: 537

Refactoring this viewController is on my todo list, but this should help out.

The key is setCollectionViewDataSourceDelegate() inside of willDisplayCell(). This lets you tap into the cell, and into the collectionView. And you can do something like set the tag to the row number.

TableView Cell:

import UIKit

class CollectionViewInsideTableViewCell: UITableViewCell {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        collectionView.register(UINib(nibName: "cellNibName", bundle: nil), forCellWithReuseIdentifier: "cellIdentifier")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
    }
    
    func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forSection section: Int, multipleSelection: Bool) {
        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.allowsSelection = true // If Needed
        collectionView.allowsMultipleSelection = multipleSelection // If Needed
        
        collectionView.scrollToItem(at: IndexPath(item: 31, section: 0), at: .right, animated: false)
        self.collectionView.layoutSubviews()
    }
    
}

Implementation:

cellforRow() {
    let collectionCell = CollectionViewInsideTableViewCell // ETC
    return collectionCell
}


extension TableViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let tableViewCell = cell as? CollectionViewInsideTableViewCell else { return }
        tableViewCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forSection: indexPath.section, multipleSelection: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = CollectionViewCell // ETC
        return cell
    }

Upvotes: 0

Sauvik Dolui
Sauvik Dolui

Reputation: 5660

To an extension of Subramanian Mariappan's concept and using delegate pattern to get informed in UIViewController about selection in collection view cell, please check your solution at https://github.com/sauvikapple/StackoverflowQ63802523.

enter image description here

Upvotes: 2

Related Questions