Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UITableViewCell中处理AVPlayer?_Ios_Swift - Fatal编程技术网

Ios 如何在UITableViewCell中处理AVPlayer?

Ios 如何在UITableViewCell中处理AVPlayer?,ios,swift,Ios,Swift,我正在努力研究如何在UITableViewCell中成功使用AVPlayer。在我的UITableViewCell中,我有一个UIView子类,其中包含一个AVPlayerLayer,使用此代码(由Apple提供): 我使用以下代码在-cellforrowatinexpath:中设置播放器: let videoPlayer = AVPlayer(playerItem: AVPlayerItem(asset: cellVideo)) cell.playerView.player = videoPl

我正在努力研究如何在
UITableViewCell
中成功使用
AVPlayer
。在我的
UITableViewCell
中,我有一个
UIView
子类,其中包含一个
AVPlayerLayer
,使用此代码(由Apple提供):

我使用以下代码在
-cellforrowatinexpath:
中设置
播放器

let videoPlayer = AVPlayer(playerItem: AVPlayerItem(asset: cellVideo))
cell.playerView.player = videoPlayer

这很好,但是,当手机看不见时,我不想播放视频。我应该将
player
设置为nil,然后当单元格再次显示时,再次设置
player
,还是暂停视频?或者我应该使用其他方法吗?

您可以实现此UITableViewDelegate方法,以便在单元格不在视图中时执行所需操作:

optional func tableView(_ tableView: UITableView, 
   didEndDisplaying cell: UITableViewCell, 
           forRowAt indexPath: IndexPath) {

}
我个人会停止视频并将播放器设置为nil,因为当单元格可见时,会调用
cellForRowAt indexPath

编辑

是的,您应该将播放器设置为零以提高性能。 我认为您还应该通过重写prepare for reuse方法,在自定义单元类中将player设置为nil

override func prepareForReuse() {
    player = nil
    super.prepareForReuse()
}
另外,如果你的目标是模仿新闻源的行为,我建议你在某个特定的时间看看它,让我们看看你的拇指


您可以使用委派方法暂停和恢复播放机。 只需确保将UITableViewCell强制转换为自定义类即可

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    //cast the cell as your custom cell
    if let myCell = cell as? MyCustomCell
    {
        myCell.player!.play()
    }

}


func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    //cast the cell as your custom cell
    if let myCell = cell as? MyCustomCell
    {
        myCell.player!.pause()
    }

}

是的,但我想问的是,我应该在这个函数中做什么?如何在cellForRowAt indexPath中设置视图?当视图再次可见时,会调用cellForRowAt indexPath,因此如果您总是在其中初始化播放器,您可能应该在DiEndDisplaying方法中将其设置为nil是的,我意识到我可以暂停它,但我想问的是,我应该暂停还是应该把球员设置为零。考虑到该单元可以用于视频以外的其他用途,而且有很多单元,我明白你的意思。我更新了我的答案,从性能角度考虑。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    //cast the cell as your custom cell
    if let myCell = cell as? MyCustomCell
    {
        myCell.player!.play()
    }

}


func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    //cast the cell as your custom cell
    if let myCell = cell as? MyCustomCell
    {
        myCell.player!.pause()
    }

}