title: How to create AVPlayer Layer in tableView Cell from outside tableView func?
body: <p>I am making an instagram-like app where I want to show a cover image for a video, then when the play button (on the image) is tapped, the video plays. I am able to create and show the posts as desired, however when I tap the play button, the video plays (I can hear the audio), but it's not visible. </p>
  
  <p>I am trying to display the video in case 1: below</p>
  
  <p>I am handling the play button function outside the tableView in an @bjc func. </p>
  
  <p>I think my problem is setting the playerLayer.frame to the desired cell/view, but I'm not entirely sure. I would appreciate any help. Here's my code. Thanks!</p>
  
  <p>UPDATED w/ SOLUTION:
  Thanks to a nudge in the right direction I learned that to access the cell of the tableview I needed to use the sender property. In this case sender.superview gave me the view I was trying to access. </p>
  
  <p>Code updated below with correction. Thanks for the help!</p>
  
  <pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let post = posts[indexPath.section]
  
      switch indexPath.row {
      case 0:
          let cell = tableView.dequeueReusableCell(withIdentifier: "PostHeaderCell") as! PostHeaderCell
          cell.userProfileImage.sd_setImage(with: post.userPhotoURL, placeholderImage: #imageLiteral(resourceName: "NinjaIcon"), options: [], completed: nil)
          cell.usernameLabel.text = post.username
  
          return cell
  
      case 1:
          let cell = tableView.dequeueReusableCell(withIdentifier: "PostImageCell") as! PostImageCell
          cell.postImage.sd_setImage(with: post.photoURL, placeholderImage: #imageLiteral(resourceName: "photo-7"), options: [], completed: nil)
          cell.playButton.isHidden = false 
          cell.playButton.addTarget(self, action: #selector(handlePlay), for: .touchUpInside)
          cell.playButton.tag = indexPath.section
  
  
          return cell
  
      case 2:
          let cell = tableView.dequeueReusableCell(withIdentifier: "PostActionCell") as! PostActionCell
          cell.delegate = self
          configureCell(cell, with: post)
  
          return cell
  
      default:
          fatalError("Error: unexpected indexPath")
      }
  }
  
  @objc func handlePlay(sender: UIButton!) {
  
      let cell = sender.superview
  
      let tag = sender.tag
      let post = posts[tag]
      let text = post.postText
      let videoURLString = post.videoURL
      let player = AVPlayer(url: videoURLString!)
  
      let playerLayer = AVPlayerLayer(player: player)
      playerLayer.frame = cell.postImage.bounds
      cell.postImage.layer.addSublayer(playerLayer)
  
      player.play()
  
      // test to make sure the right video is playing
      print("for text: ",text)
      print("Attempting to Play: ",videoURLString )
  
  }
  </code></pre>