UITableViewCell 中的 AVPlayer 导致 "Unexpectedly found nil while implicitly unwrapping an Optional value" 错误
AVPlayer in UITableViewCell results in "Unexpectedly found nil while implicitly unwrapping an Optional value" error
我正在尝试设置将在单元格中包含视频播放器的自定义 UITableViewCell。
我收到一个 "Unexpectedly found nil while implicitly unwrapping an Optional value"
在线:
"playerLayer.frame = self.videoPlayerSuperView.bounds"
这是我使用的代码:
import UIKit
import AVFoundation
class feedCell: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var viewsLabel: UILabel!
@IBOutlet weak var postTextLabel: UILabel!
@IBOutlet weak var videoPlayerSuperView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let videoURL = URL(string: "https://some.url.of.an.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoPlayerSuperView.bounds
self.videoPlayerSuperView.layer.addSublayer(playerLayer)
player.play()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我该如何解决这个问题?
尝试添加延迟
override func awakeFromNib()
{
super.awakeFromNib()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2 ,
execute:
{
let videoURL = URL(string: "https://some.url.of.an.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoPlayerSuperView.bounds
self.videoPlayerSuperView.layer.addSublayer(playerLayer)
player.play()
})
}
感谢@AjinkyaSharma
override func layoutSubviews() {
super.layoutSubviews()
let videoURL = URL(string: "https://some.url.of.an.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoPlayerSuperView.bounds
self.videoPlayerSuperView.layer.addSublayer(playerLayer)
player.play()
}
我正在尝试设置将在单元格中包含视频播放器的自定义 UITableViewCell。
我收到一个 "Unexpectedly found nil while implicitly unwrapping an Optional value"
在线:
"playerLayer.frame = self.videoPlayerSuperView.bounds"
这是我使用的代码:
import UIKit
import AVFoundation
class feedCell: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var viewsLabel: UILabel!
@IBOutlet weak var postTextLabel: UILabel!
@IBOutlet weak var videoPlayerSuperView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
let videoURL = URL(string: "https://some.url.of.an.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoPlayerSuperView.bounds
self.videoPlayerSuperView.layer.addSublayer(playerLayer)
player.play()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
我该如何解决这个问题?
尝试添加延迟
override func awakeFromNib()
{
super.awakeFromNib()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2 ,
execute:
{
let videoURL = URL(string: "https://some.url.of.an.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoPlayerSuperView.bounds
self.videoPlayerSuperView.layer.addSublayer(playerLayer)
player.play()
})
}
感谢@AjinkyaSharma
override func layoutSubviews() {
super.layoutSubviews()
let videoURL = URL(string: "https://some.url.of.an.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.videoPlayerSuperView.bounds
self.videoPlayerSuperView.layer.addSublayer(playerLayer)
player.play()
}