Ios 视频播放时未调用UITableView显示
我已经试图在这里找到一个类似的问题,但没有成功 我有一个包含各种内容和单元格的UITableView。特别是,它可以显示带有WKWebView的自定义UITableViewCell。 有时,当我在willDisplayCell上配置单元格时,此webview会自动播放视频。 由于此自动播放功能,当我向上/向下滚动时,单元格不会调用委托方法didendisplayingcell,并且视频在后台保持活动状态。用户需要关闭viewController或整个应用程序Ios 视频播放时未调用UITableView显示,ios,swift,uitableview,video,webview,Ios,Swift,Uitableview,Video,Webview,我已经试图在这里找到一个类似的问题,但没有成功 我有一个包含各种内容和单元格的UITableView。特别是,它可以显示带有WKWebView的自定义UITableViewCell。 有时,当我在willDisplayCell上配置单元格时,此webview会自动播放视频。 由于此自动播放功能,当我向上/向下滚动时,单元格不会调用委托方法didendisplayingcell,并且视频在后台保持活动状态。用户需要关闭viewController或整个应用程序 我尝试使用webview在列表中保
我尝试使用webview在列表中保存所有出列的单元格,并在ScrollViewDiEndDecelling上使用以下方法手动将已保存单元格的webview内容设置为html“”: 通过这种方式,webview会正确地停止在后台播放视频,但问题是,即使使用这种方法,单元格也不会排队,并且不会调用DiEndDisplayingCell:,因此当我再次滚动到该单元格时,我无法重新进行正确的配置以重新加载webview
你有什么建议或窍门吗?我找到了一个解决办法。基本上,由于某种奇怪的原因,关于webView在这种情况下如何处理视频,该单元不会被回收。 因此,我将所有这些单元格作为实例属性保存在
Set
中,并在scrollViewDidScroll(:)
中检查单元格是否仍然可见。如果不可见,我会手动将webView从其superview中删除
var advTableViewCells = Set<AdvertisingTableViewCell>() // here I save the cell retrieved in `tableView(_:cellForRowAt:)`
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var cellsToRemove = Set<AdvertisingTableViewCell>() // save the cell to remove in a temporary set, to avoid removing element in a forEach
advTableViewCells.forEach { cell in
if !tableView.visibleCells.contains(cell) {
cell.webView?.stopLoading()
cell.webView?.removeFromSuperview()
cell.webView = nil
cellsToRemove.insert(cell)
}
}
cellsToRemove.forEach { cell in
self.advTableViewCells.remove(cell)
}
}
var advTableViewCells=Set()//在这里,我保存了在“tableView(u:cellForRowAt:)”中检索到的单元格`
func scrollViewDidScroll(scrollView:UIScrollView){
var cellsToRemove=Set()//将要删除的单元格保存在临时集中,以避免删除forEach中的元素
advTableViewCells.forEach{中的单元格
if!tableView.visibleCells.contains(单元格){
cell.webView?.stopLoading()
cell.webView?.removeFromSuperview()
cell.webView=nil
cellsToRemove.insert(单元格)
}
}
cellsToRemove.forEach{中的单元格
self.advTableViewCells.remove(单元格)
}
}
@RicoCrescenzio这样,单元格是否得到重用,prepareforuse()
被调用,以及didendisplay…
(当然,仅在其无web状态下)?@AmitaiB据我所知,是的,这些方法被调用
var advTableViewCells = Set<AdvertisingTableViewCell>() // here I save the cell retrieved in `tableView(_:cellForRowAt:)`
func scrollViewDidScroll(_ scrollView: UIScrollView) {
var cellsToRemove = Set<AdvertisingTableViewCell>() // save the cell to remove in a temporary set, to avoid removing element in a forEach
advTableViewCells.forEach { cell in
if !tableView.visibleCells.contains(cell) {
cell.webView?.stopLoading()
cell.webView?.removeFromSuperview()
cell.webView = nil
cellsToRemove.insert(cell)
}
}
cellsToRemove.forEach { cell in
self.advTableViewCells.remove(cell)
}
}