title: Destroying scrolled Video cells in UICollectionView for TikTok-like functionality
body: <p>Trying to achive a TikTok-like functionality with fullscreen video views
  I am relatively new to swift so please pardon me
  using the following player <a href="https://github.com/piemonte/Player" rel="nofollow noreferrer">https://github.com/piemonte/Player</a></p>
  
  <p>Having difficulty destroying earlier cell instances as a result when the app moves from background to foreground, all video instances start to play</p>
  
  <p>Following is my view controller</p>
  
  <pre><code>import Player
  import UIKit
  
  class HomeViewController: UIViewController,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource  {
  
  
      lazy var data :[homeModel] =  [
          homeModel(charge: 231,videoUrl:"vineet_179_2",currency:"rupees",tname:"Abhinav Mahajan",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"content,inthenight creator,Insad"),
  
          homeModel(charge: 2331,videoUrl:"vineet_179_2",currency:"rupees",tname:"Kuwber Singh",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Footballer"),
  
          homeModel(charge: 231,videoUrl:"vineet_179_2",currency:"rupees",tname:"Harsh Singh",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Footballer,Koolboi"),
  
          homeModel(charge: 11,videoUrl:"vineet_179_2",currency:"rupees",tname:"Sameeh Hola",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Singer,Footballer"),
  
          homeModel(charge: 43231,videoUrl:"vineet_179_2",currency:"rupees",tname:"Baag Singh",backgroundImage: #imageLiteral(resourceName: "3"),mDesc:"Cricketer"),
      ]
  
       let collectionView:UICollectionView = {
  
          let layout = UICollectionViewFlowLayout()
          layout.scrollDirection = .vertical
          layout.minimumLineSpacing = 0
  
          layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  
          let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
  
          cv.translatesAutoresizingMaskIntoConstraints = false
          cv.register(CustomCell.self, forCellWithReuseIdentifier: "cell")
  
          return cv
      }()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          collectionView.reloadData()
          self.title = "HomeView"          
          view.addSubview(collectionView)
          collectionView.backgroundColor = .red
          collectionView.delegate = self
          collectionView.dataSource = self
          collectionView.showsVerticalScrollIndicator = false //
          collectionView.isPagingEnabled = true
          print("--colelction view --")
  
  
          if #available(iOS 11.0, *) {
              collectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
              collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
              collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
              collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
          } else {
              collectionView.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
              collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
              collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
              collectionView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor).isActive = true //fix
          }
  
  
  
      }
  
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return data.count
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
              print("\(indexPath.row)")
  
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
              cell.configure(with: data[indexPath.row])
              print("---cellForItemAt--")
              return cell
      }
  
  
      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  
          print("willDisplay")
      }
  
  
      func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
          (cell as? CustomCell)?.player.pause()
          self.collectionView(collectionView, willDisplay: cell, forItemAt: indexPath)
      }
  
  
      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  
          collectionView.visibleCells.forEach { cell in
  
              (cell as? CustomCell)?.player.playFromBeginning()
              print("scrollViewDidEndDecelerating")
          }
      } 
  }
  
  </code></pre>
  
  <p>Following is custom cell</p>
  
  <pre><code>import UIKit
  import Player
  
  
  class CustomCell: UICollectionViewCell {
  
  
      let indianCurrency = "₹"
      var localUrl = Bundle.main.url(forResource:"", withExtension: "")
  
  
      public func configure(with model: homeModel) {
          self.localUrl = Bundle.main.url(forResource:"\(model.videoUrl)", withExtension: "mp4")
          self.player.url = localUrl
          self.button.setTitle("Book now for \(indianCurrency)\(model.charge)", for: .normal)
          self.nameLabel.text = ("\(model.tname)")
  
          self.marqueeLabel.text = ("\(model.mDesc)")
          print("cell configure called")
      }
  
  
  
  
      override func prepareForReuse() {
          super.prepareForReuse()
          self.localUrl = Bundle.main.url(forResource:"", withExtension: "MOV")
      }
  
  
      let player: Player = {
          let homePlay = Player()
          homePlay.playerView.playerBackgroundColor = .cyan
          homePlay.playerView.translatesAutoresizingMaskIntoConstraints = false
          homePlay.playbackLoops = true
          homePlay.url = Bundle.main.url(forResource: "re", withExtension: "mp4")
          return homePlay
      }()
  
      override init(frame: CGRect) {
          super.init(frame: .zero)
          setupView()
  
          setupPlayerView()
      }
  
      func setupView(){
  
          contentView.backgroundColor = .green
          contentView.addSubview(homeView)
  
          homeView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
          homeView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
          homeView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
          homeView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
          homeView.backgroundColor = .green
  
      }
  
      func setupPlayerView(){
          homeView.addSubview(self.player.playerView)
          player.playerView.isHidden = false
          player.playerView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
          player.playerView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
          player.playerView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
          player.playerView.heightAnchor.constraint(equalTo: contentView.heightAnchor).isActive = true
          player.playerView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
          let tapGestureRecognizer: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapGestureRecognizer(_:)))
          tapGestureRecognizer.numberOfTapsRequired = 1
          player.view.addGestureRecognizer(tapGestureRecognizer)
      }
  
      required init?(coder aDecoder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
  
  }
  
  extension CustomCell {
  
      @objc func handleTapGestureRecognizer(_ gestureRecognizer: UITapGestureRecognizer) {
          switch self.player.playbackState {
          case .stopped:
              self.player.playFromBeginning()
              break
          case .paused:
              self.player.playFromCurrentTime()
              break
          case .playing:
              self.player.pause()
              break
          case .failed:
              self.player.pause()
              break
          }
      }
  }
  
  </code></pre>
  
  <p>Also, This is the Model I am using to play videos locally</p>
  
  <pre><code>struct homeModel {
      var charge:Int
      var videoUrl:String
      var currency:String
      var tname:String
      var backgroundImage: UIImage
      var mDesc:String
  }
  
  struct Video {
  
      let url: URL?
  
      init(url: URL?) {
          self.url = url
      }
  }
  
  </code></pre>
  
  <p>Can anyone please tell me what I am missing?</p>