无法加载具有 UICollectionVIewCell 数据的 UIView



我到处寻找解决方案。写下这个问题只是再次碰碰运气。我有一个collectionViewCell,我在其中解析了一个XML URL数据,其中包括一个名称和一个ID。现在,当我点击该单元格时,我想加载一个包含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()函数中获取didSet中设置的属性?

选择单元格时,可能需要检索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()
}
}
}

PlayerView类中的项属性从未在初始值设定项中设置。您需要在那里设置它,或者让任何创建PlayerView实例的类设置项属性。否则它将永远为零。

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

最新更新