无法使用 UICollectionVIewCell 数据加载 UIView
Can't load a UIView with UICollectionVIewCell data
我到处寻找解决办法。通过写这个问题再次尝试我的运气。我有一个 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 class 中设置了项目,但是在 select 单元格之后,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 中的一些代码 class:
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
当我 select 项目时。如何在 init
或 startPlaying()
func 中获取 didSet
中设置的属性?
您的 PlayerView class 中的项目 属性 从未在您的初始化程序中设置。您要么需要在那里设置它,要么让任何创建 PlayerView 实例的 class 设置项目 属性。否则它将永远为零。
您可能想在选择单元格时检索 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()
}
}
}
终于修好了!以下是我在代码中所做的更改:
在 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 中,我将 init
和 didSet
编辑为:
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()
希望它能对以后的人有所帮助,所以在这里发布解决方案。大2感谢@sCha帮助我!
我到处寻找解决办法。通过写这个问题再次尝试我的运气。我有一个 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 class 中设置了项目,但是在 select 单元格之后,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 中的一些代码 class:
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
当我 select 项目时。如何在 init
或 startPlaying()
func 中获取 didSet
中设置的属性?
您的 PlayerView class 中的项目 属性 从未在您的初始化程序中设置。您要么需要在那里设置它,要么让任何创建 PlayerView 实例的 class 设置项目 属性。否则它将永远为零。
您可能想在选择单元格时检索 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()
}
}
}
终于修好了!以下是我在代码中所做的更改:
在 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 中,我将 init
和 didSet
编辑为:
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()
希望它能对以后的人有所帮助,所以在这里发布解决方案。大2感谢@sCha帮助我!