无法使用UICollectionVIewCell数据

时间:2017-08-29 16:12:15

标签: xml swift uiview xml-parsing uicollectionviewcell

我到处寻找解决方案。通过写这个问题再试一次我的运气。我有一个collectionViewCell,我在其中解析了包含名称和ID的XML URL数据。现在,当我点击单元格时,我想加载一个带有AVPlayer的UIView。要播放该文件,我必须使用不同的URL但使用我在该单元格中解析的id。这是我的didSelectItem func

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let playerLauncher = PlayerLauncher()
        playerLauncher.showPlayer()
 }

这是我的PlayerLauncher文件:

import UIKit

import AVFoundation

class PlayerView: UIView {

    var item: Item?

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = UIColor.black

        let id = item?.itemId

        if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
            let player = AVPlayer(url: url)
            print(url)
            let playerLayer = AVPlayerLayer(player: player)
            self.layer.addSublayer(playerLayer)
            playerLayer.frame = self.frame

            player.play()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class PlayerLauncher: NSObject {

        func showPlayer() {

            if let keyWindow = UIApplication.shared.keyWindow {
                let view = UIView(frame: keyWindow.frame)
                view.backgroundColor = UIColor.white

                view.frame = CGRect(x: 0, y: keyWindow.frame.height - 50, width: view.frame.width, height: 50)

                let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
                let playerView = PlayerView(frame: playerFrame)
                view.addSubview(playerView)

                keyWindow.addSubview(view)

                UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                    view.frame = keyWindow.frame
                }, completion: nil)
            }
        }
    }

我已经从我的Item NSObject类中设置了该项,但在选择单元格后,UIView加载正常,但不播放该文件并显示id = nil。我该如何解决?

更新:所以我稍微更改了我的代码,我可以打印ID。

以下是我在didSelectItem方法中所做的更改:

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

        collectionView.deselectItem(at: indexPath, animated: true)

        let playerView = PlayerView()
        playerView.item = items?[indexPath.item]

        let playerLauncher = PlayerLauncher()
        playerLauncher.showPlayer()
 }

之后我更改了PlayerView和PlayerLauncher类中的一些代码:

import UIKit
import AVFoundation

class PlayerView: UIView {

    var id: String?

    var item: Item? {
        didSet {
            id = item?.itemId
            print(id)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        backgroundColor = UIColor.black
    }

    func startPlaying() {

        if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
            let player = AVPlayer(url: url)
            print(url)
            let playerLayer = AVPlayerLayer(player: player)
            self.layer.addSublayer(playerLayer)
            playerLayer.frame = self.frame

            player.play()
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class PlayerLauncher: NSObject {

    func showPlayer() {

        if let keyWindow = UIApplication.shared.keyWindow {
            let view = UIView(frame: keyWindow.frame)
            view.backgroundColor = UIColor.white

            view.frame = CGRect(x: 0, y: keyWindow.frame.height - 50, width: view.frame.width, height: 50)

            let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
            let playerView = PlayerView(frame: playerFrame)
            view.addSubview(playerView)

            keyWindow.addSubview(view)

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                view.frame = keyWindow.frame
            }, completion: { (completeAnimation) in
                playerView.startPlaying()
            })
        }
    }
}

现在我可以打印来自didSet的ID,但在startPlaying()方法中,当我选择项目时,它仍会显示id = nil。如何在didSetinit func中的startPlaying()中设置属性?

3 个答案:

答案 0 :(得分:0)

您的PlayerView类中的item属性永远不会在初始化程序中设置。您需要在那里设置它或者让任何创建PlayerView实例的类设置item属性。否则它将永远是零。

答案 1 :(得分:0)

您可能希望在选择单元格时检索Item信息。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let item: Item = itemList[indexPath.item]   // just for instance
    let playerLauncher = PlayerLauncher()
    playerLauncher.showPlayer(item)
}

因此您需要更改Player之类的内容:

class PlayerLauncher: NSObject {

    func showPlayer(item: Item) {

        if let keyWindow = UIApplication.shared.keyWindow {
            let view = UIView(frame: keyWindow.frame)
            view.backgroundColor = UIColor.white

            view.frame = CGRect(x: 0, y: keyWindow.frame.height - 50, width: view.frame.width, height: 50)

            let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
            let playerView = PlayerView(frame: playerFrame)
            playerView.item = item // Here
            view.addSubview(playerView)

            keyWindow.addSubview(view)

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                view.frame = keyWindow.frame
            }, completion: { _ in  
                playerView.startPlaying()  // And here
            })
        }
    }
}

PlayerView

class PlayerView: UIView {

    var item: Item?
    var player: AVPlayer?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.black
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func startPlaying() {
        let id = item?.itemId

        if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
            self.player = AVPlayer(url: url)
            print(url)
            let playerLayer = AVPlayerLayer(player: self.player)
            self.layer.addSublayer(playerLayer)
            playerLayer.frame = self.frame

            self.player.play()
        }
    }
}

<强>更新:

试试看看会发生什么:

func startPlaying() {
    guard let id = id else { print("** id is nil"); return }  // here
    print("* item id = \(id)")         // and here

    if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
        let player = AVPlayer(url: url)
        print(url)
        let playerLayer = AVPlayerLayer(player: player)
        self.layer.addSublayer(playerLayer)
        playerLayer.frame = self.frame

        player.play()
    }
}

更新#2:

再次,整个代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let item = items?[indexPath.item]
    let playerLauncher = PlayerLauncher()
    playerLauncher.showPlayer(item)
}

class PlayerLauncher: NSObject {

    func showPlayer(item: Item) {

        if let keyWindow = UIApplication.shared.keyWindow {
            let view = UIView(frame: keyWindow.frame)
            view.backgroundColor = UIColor.white

            view.frame = CGRect(x: 0, y: keyWindow.frame.height - 50, width: view.frame.width, height: 50)

            let playerFrame = CGRect(x: 0, y: 0, width: keyWindow.frame.width, height: keyWindow.frame.height)
            let playerView = PlayerView(frame: playerFrame)
            playerView.item = item // Here
            view.addSubview(playerView)

            keyWindow.addSubview(view)

            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
                view.frame = keyWindow.frame
            }, completion: { _ in  
                playerView.startPlaying()  // And here
            })
        }
    }
}

class PlayerView: UIView {

    var id: String?
    var item: Item? {
        didSet {
            id = item?.itemId
            print(id)
        }
    }
    var player: AVPlayer?

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.black
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func startPlaying() {
        guard let id = id else { print("** id is nil"); return }  // here
        print("* item id = \(id)")         // and here

        if let url = URL(string: "http://xample.com/play.m3u?id=\(id)") {
            self.player = AVPlayer(url: url)
            print(url)
            let playerLayer = AVPlayerLayer(player: self.player)
            self.layer.addSublayer(playerLayer)
            playerLayer.frame = self.frame

            self.player.play()
        }
    }
}

答案 2 :(得分:0)

终于修好了!以下是我在代码中所做的更改: 在didSelectItem方法中:

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

     if let item = items?[indexPath.item] {
        showPlayer(item: item)
     }
}

func showPlayer(item: Item) {

     let playerLauncher = PlayerLauncher()
     playerLauncher.item = item
     playerLauncher.showVideoPlayer()
}

在PlayerView中,我将initdidSet编辑为:

var item = Item()

init(frame: CGRect, item: Item) {
    super.init(frame: frame)
    self.item = item

    setupPlayerView()
    backgroundColor = UIColor(white: 0.3, alpha: 1)
}

setupPlayerView方法中,我设置了itemId,最后在我刚添加的PlayerLauncher中设置:

var item = Item()

希望将来对某人有所帮助,以便在此处发布解决方案。非常感谢@sCha帮助我!