0

I have taken collection view in tableview cell now when I click the collection view cell it will go to the next view controller,is it possible? I have tried did select item method in that how to use perform segue method

1 Answers1

1

I am showing u example

     class ViewController: UIViewController, UITableViewDelegate,
     UITableViewDataSource {
         @IBOutlet weak var tableView: UITableView!  
    override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
        return false
    }
 }

This ViewController contain Tableview datasource n delegates. Its cell of type DemoTableViewCell showing below

    class DemoTableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate {
        var parentVC: UIViewController?

        @IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let demoCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DemoCollectionViewCell", for: indexPath) as! DemoCollectionViewCell
        return demoCollectionViewCell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
        (self.parentVC as! ViewController).performSegue(withIdentifier: "YourSegueName", sender: self)


    }
    }

Again DemoTableViewCell contain datasouce and delegate of collection view as showing above Also didSelectItemAt contains performSegue, which is using parent refrence for Segue.

Now in collection view cell, there is 1 variable named parentVC, U have to set this value in cellForRowatIndexpath defined in ViewController class like that

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let demoTableViewCell = tableView.dequeueReusableCell(withIdentifier: "DemoTableViewCell", for: indexPath) as! DemoTableViewCell
        demoTableViewCell.parentVC = self
        demoTableViewCell.collectionView.reloadData()
        return demoTableViewCell
    }

and final make a segue from collectionViewCell on storyboard to YourController

  • I need collectionview in first section of tableview cell and remaing are not having collectionviews , when click second section tableview cell it performs prepare for segue ,now my question is you wrote should perform is false , if we give false ,will tableview perform prepare for segue method? – udaykumar40 May 22 '17 at 06:51
  • In that case, you need to call performSegue method when u click in any table Cell like that self.performSegue(withIdentifier: "your_segue_identifier", sender: self) and shouldPerformSegue will remain return false. – hament miglani May 22 '17 at 06:58
  • Thank you for your answer. I had an similar issue, and I solved it. I think that key part of this answer is setting `var parentVC: UIViewController?` inside View and, set that controller from `cellForRowAt` by `demoTableViewCell.parentVC = self`. – Changnam Hong May 24 '17 at 11:49