我使用自定义cell
在单元格中播放视频。但是它所做的只是显示给我空的tableView
cell
,有人可以帮助我吗?
override func viewDidLoad() {
super.viewDidLoad()
let Custome = CustomeNavigation(nibName: "CustomeNavigation", bundle: nil)
self.view.addSubview(Custome.view)
Custome.lbl_customeTitle.text = "FEED"
tableView.frame = CGRect(x:0, y:0, width:view.bounds.width, height:view.bounds.height);
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "VedioCell", for: indexPath) as! FeedCell
let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = cell.bounds
cell.layer.addSublayer(playerLayer)
player.play()
return cell
}
最佳答案
确保导入import AVKit
和import AVFoundation
。
您可以在cellForRowAtIndexPath
中尝试这样:
let cell: FeedCell = tableView.dequeueReusableCellWithIdentifier("VedioCell", forIndexPath: indexPath) as! FeedCell
let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(URL: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = cell.bounds
cell.layer.addSublayer(playerLayer)
cell?.playerView?.layer.addSublayer(playerLayer)
player.play()
return cell
如果您使用的是swift3,则语法如下:
let videoURL = NSURL(string: "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = (cell?.bounds)!
cell?.layer.addSublayer(playerLayer)
cell?.playerView?.layer.addSublayer(playerLayer)
player.play()
而且,如果要显示许多内容,还必须确保与
cellForRowAtIndexPath
打交道。