无法使用 UICollectionVIewCell 数据加载 UIView

Posted

技术标签:

【中文标题】无法使用 UICollectionVIewCell 数据加载 UIView【英文标题】:Can't load a UIView with UICollectionVIewCell data 【发布时间】:2017-08-29 16:12:14 【问题描述】:

我到处寻找解决方案。通过写这个问题再次尝试我的运气。我有一个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。如何在initstartPlaying() func 中获取didSet 中设置的属性?

【问题讨论】:

【参考方案1】:

您可能希望在选择单元格时检索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()
        
    

【讨论】:

您好!它仍然显示 id = nil。我需要对我的 PlayerView 类进行任何更改吗?因为这是我调用 AVPlayer 播放链接的地方。 @Mohit 您的PlayerView 似乎在初始化时开始播放,抱歉我看了一眼代码,所以您需要在init 上传递item 或更改它的工作方式。我将在我的答案中添加一些代码。 @Mohit 还有一件事。我相信您的 AVPlayer 实例应该在类中全局声明。也添加到我的答案中。 我进行了所有更改,但仍然显示 id = nil。 :( @Mohit 这个类对我来说看起来不错,但我无法调试比这更深的东西,因为我无法自己测试它。如果它确实存在,请尝试查看collectionView 的委托方法中的item 对象。祝你好运!【参考方案2】:

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

【讨论】:

感谢您的回答。你能告诉我如何创建它的实例吗?【参考方案3】:

终于修好了!以下是我在代码中所做的更改: 在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 帮助我!

【讨论】:

以上是关于无法使用 UICollectionVIewCell 数据加载 UIView的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 AutoLayout 设置 UICollectionViewCell 的宽度

无法在 UITableViewCell 中聚焦 UICollectionViewCell

UICollectionViewCell 中的 UILabel 有时无法正确调整大小

无法在 xib 文件中选择 UICollectionViewCell

自动布局:无法获取 UICollectionViewCell 子视图的帧大小

在 UICollectionViewCell 的情况下无法同时满足约束