title: Collection View Cell Action using Delegates and Protocols
body: <p>I have created a View Controller that contains a UITableView and each UITableViewCell contains a UICollectionView.</p>
  
  <p>I made 2 API calls and every collectionView present the first 5 results of each API call.</p>
  
  <p>Also, I added a button on each TableView Header on the right corner with the title "Show All". You can see the screen on the Image below.</p>
  
  <p>Here is how I add the tableView header button:</p>
  
  <pre><code>func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
      let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 100))
  
      let showHideButton: UIButton = UIButton(frame: CGRect(x:headerView.frame.size.width - 80, y:0, width:75, height:35))
      showHideButton.setTitle("Show All", for: .normal)
      showHideButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
      showHideButton.setTitleColor(#colorLiteral(red: 0.9607843137, green: 0.007843137255, blue: 0.03137254902, alpha: 1), for: .normal)
      //showHideButton.addTarget(self, action: #selector(btnShowHideTapped), for: .touchUpInside)
  
      headerView.addSubview(showHideButton)
  
      return headerView
  }
  </code></pre>
  
  <blockquote>
    <p>When I tap on "show all" button on tableView header, I want to jump to another view controller ("showAllViewController") and represent all the result of my object and when I tap on the Image of CollectionViewCell I want to jump to another view controller ("detailsViewController"). How can I do it using delegates and protocols?</p>
  </blockquote>
  
  <p>Here is an example image with my screen:</p>
  
  <p><a href="https://i.stack.imgur.com/O931Z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O931Z.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Edit</strong>: I followed the following steps from this question (<a href="https://stackoverflow.com/questions/45030010/navigate-on-click-of-collectionview-cell-inside-tableview">navigate on click of collectionview cell inside tableview</a>) but I don't know what I need to write on the "cellTapped()" function:</p>
  
  <p>ViewController.swift :</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
          cell.delegate = self
          return cell
      }
  </code></pre>
  
  <p>MyCell.swift :</p>
  
  <pre><code>protocol CategoryRowDelegate:class {
  func cellTapped()
  }
  </code></pre>
  
  <p>CategoryRow.swift :</p>
  
  <pre><code>class CategoryRow : UITableViewCell {
       weak var delegate:CategoryRowDelegate?
      @IBOutlet weak var collectionView: UICollectionView!
  }
  
   func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  if delegate!= nil {
  delegate?.cellTapped()
  }
  }
  </code></pre>
  
  <p>Add the delegate function inside ViewController</p>
  
  <pre><code>func cellTapped(){
  //code for navigation
  //I don't know what to write
  }
  </code></pre>
  
  <p>Can anybody help me?</p>