Search code examples
swiftxcodecollectionview

perform segue from collection view inside table view


I have a table view with a collection view in my 2nd cell.

When a video from the collection view is clicked, i want to perform a segue to another view controller.

enter image description here


Solution

  • that's the sample code.

    class tableViewClass: UIViewController, UITableViewDelegate, UITableViewDataSource, VideoCellSelectionDelegate {
        @IBOutlet weak var tableView: UITableView!
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell", for: indexPath) as! VideoCell
            cell.delegate = self
            return cell
        }
    
        func didSelect() {
            //performSegue
        }
    
    }
    

    the VideoCell must have a delegate which is called when a collectionView cell is select

    protocol VideoCellSelectionDelegate {
        func didSelect()
    }
    
    class VideoCell: UITableViewCell, UICollectionViewDelegate {
        var delegate: VideoCellSelectionDelegate?
    
        @IBOutlet weak var collectionView: UICollectionView!
    
        func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
            delegate?.didSelect()
        }
    
    }