如何修正我的程式码:点选uicollectionview储存格时,我想更新影片

时间:2019-04-25 20:25:07

标签: ios uicollectionview swift4.2

我正在创建一个舞蹈学习应用程序。我的第一个View Controller是一个包含单元格中图像的集合视图。点击单元格时,它将打开每个单元格的数据。之后,在第二个View Controller中选择单元格时,我将更新所有视图,并且我的标签也将更新。但是我的第二个视图控制器上还有一个视频按钮。问题是我想在选择单元格时更新所选视频按钮的视频。

我已经用Google搜索,但没有找到任何可以帮助我的东西。

这是我的第二个视图控制器。

import UIKit
import AVKit

let detailCollectionViewReuseIdentifier = "detailCollectionViewCell"

class DetailViewController: UIViewController {

    @IBOutlet weak var detailCollectionView: UICollectionView!
    @IBOutlet weak var navItem: UINavigationItem!
    @IBOutlet weak var danceLearnName: UILabel!
    @IBOutlet weak var courseButton: UIButton!
    @IBOutlet weak var detailDanceDescription: UITextView!
    @IBOutlet weak var mediaDanceName: UIButton!

    var detailDance: Dance? {
        didSet {
            configureView()
        }
    }

    func configureView() {
        if let detailDance = detailDance {
            if let navItem = navItem, let danceLearnName = danceLearnName, let courseButton = courseButton, let detailDanceDescription = detailDanceDescription, let mediaDanceName = mediaDanceName {
                detailDanceDescription.text = detailDance.danceCourseDescription[0]
                danceLearnName.text = detailDance.danceCourseName[0]
                mediaDanceName.setTitle("Մեդիա", for: .normal)
                courseButton.layer.masksToBounds = true
                courseButton.layer.cornerRadius = 15
                navItem.title = detailDance.danceName + " Ուսուցում"
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        configureView()
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showVideos" {
            if let destination = segue.destination as? DanceMediaViewController {
                destination.detailDance = detailDance
            }
        }
    }

    func playMedia(_ path: String) {
        guard let videoPath = Bundle.main.path(forResource: path, ofType: "mov") else {
            debugPrint("\(String(describing: path)) not found")
            return
        }
        let player = AVPlayer(url: URL(fileURLWithPath: videoPath))
        let playerController = AVPlayerViewController()
        playerController.player = player
        present(playerController, animated: true) {
            player.play()
        }
    }

    @IBAction func mediaStepClicked() {
        let indexPath = (detailDance?.danceMediaBySteps[0])!
        playMedia(indexPath)
    }
}

extension DetailViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (detailDance?.detailDanceImages.count)!
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: detailCollectionViewReuseIdentifier, for: indexPath) as! DetailCollectionViewCell

        cell.detailDanceImages.image = UIImage(named: (detailDance?.detailDanceImages[indexPath.item])!)
        cell.detailDanceImages.layer.cornerRadius = 8

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: false)
        collectionView.delaysContentTouches = false

        detailDanceDescription.text = detailDance?.danceCourseDescription[indexPath.item]
        danceLearnName.text = detailDance?.danceCourseName[indexPath.item]

    }

    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! DetailCollectionViewCell
        cell.contentView.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
    }

    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as! DetailCollectionViewCell
        cell.contentView.backgroundColor = UIColor.clear
    }

}

我想获得有关在didSelect在我的单元格上如何集成正确功能并更新点击按钮的视频的建议。

0 个答案:

没有答案