UITableView didEndDisplaying not called when playing video

I have already tried to find a similar question here without success.

I have a UITableView with various contents and cells. In particular, it can display a custom UITableViewCell with WKWebView. Sometimes this webview has an autoplay video that starts when I set the cell to willDisplayCell . Since this autoplay function, when I scroll up / down, the cell does not call the didEndDisplayingCell delegate method , and the video remains active in the background. The user needs to close the viewController or the entire application.


I tried to save in the list all selected cells with webview and scrollViewDidEndDecelerating manually setting the contents of the saved cell web view to html "" using:

cell.webView?.loadHTMLString("", baseURL: nil)

Thus, web browsing stops correctly in order to play the video in the background, but the problem is that even with this approach, the cell does not queue and didEndDisplayingCell: is not called, so when I view this cell again, I cannot Repeat the correct configuration to reload the webview.

Do you have some tips or tricks?

+5
source share
1 answer

. , - , , webView . Set scrollViewDidScroll(_:) , . , webView .

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)
    }
}
0

Source: https://habr.com/ru/post/1692034/


All Articles