从一个单元格转到不同的控制器(详细视图和 avcontroller)

Segue to different controllers from one cell (a detail view and a avcontroller)

如果有视频,当单元格被点击时,我只想显示视频(在 avPlayerViewController 中),如果没有视频,它会执行转至详细视图

我试过这个:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


    let post = postArray[indexPath.item]
    let controller = PostDetailViewController.fromStoryboard(post: post)
    self.navigationController?.pushViewController(controller, animated: true)


    guard let videoURL = URL(string: post.videoLink)  else {
        return
            }

    let avPlayer = AVPlayer(url: videoURL)
    let avController = AVPlayerViewController()
    avController.player = avPlayer

    present(avController, animated: true) {
        avPlayer.play()
    }

}

如果有视频(来自 Firebase),它会播放视频,但它也会打开详细视图。所以我希望单元格只打开视频或只打开详细视图

构造它,如果没有 post.videoLink 则推送 PostDetailViewController,否则将播放视频:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    guard let videoURL = URL(string: post.videoLink) else {
        //If there is no URL, then this else condition will be called. Otherwise, the code below the guard statement will be called, and the video player will appear.
        let post = postArray[indexPath.item]
        let controller = PostDetailViewController.fromStoryboard(post: post)
        self.navigationController?.pushViewController(controller, animated: true)
        return
    }

    let avPlayer = AVPlayer(url: videoURL)
    let avController = AVPlayerViewController()
    avController.player = avPlayer

    present(avController, animated: true) {
        avPlayer.play()
    }

}