ios - UITableViewCell 中的 WKWebView 需要一些时间来加载(仅第一个单元格加载)
问题描述
我有一个UITableViewCell
包含一个WKWebView
(我的目标是显示单元格中的 YouTube 视频)。
这是我尝试过的:
import UIKit; import WebKit
class ViewController: UIViewController {
var displayCell = false {
didSet { updateView() }
}
@IBOutlet weak var tableView: UITableView! {
didSet { tableView.register(UINib(nibName: "VideoCell", bundle: .main),
forCellReuseIdentifier: "VideoCell") }
}
@IBAction func tapped() {
displayCell.toggle()
updateView()
}
private func updateView() {
tableView.reloadData()
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VideoCell", for: indexPath) as! VideoCell
if let url = URL(string: "https://www.youtube.com/embed/qxWwEPeUuAg") {
cell.wkWebView.load(URLRequest(url: url))
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
200
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
displayCell ? 1 : 0
}
}
final class VideoCell: UITableViewCell {
@IBOutlet weak var wkWebView: WKWebView!
}
点击按钮隐藏或显示第一个单元格。我意识到显示视频单元时会出现延迟,但只是第一次。我什至没有加载 URL 就试过了,延迟仍然存在。所以我想当WKWebView
应用程序第一次缓存单元格时有问题,当单元格出列时不会再发生这种情况。但我找不到什么。
你可以帮帮我吗?非常感谢。
解决方案
推荐阅读
- python - 使用两个 for 循环矢量化图像生成
- java - 设置 OSM 全屏的正确方法
- pdf - 如何使 pdf 或 docx 文件损坏?
- flutter - 如何在 Flutter 中拥有 2 种不同颜色的抽屉
- python - 命令行中的“SyntaxError:行继续字符后的意外字符”
- arrays - 如何用火花中的其他值替换数组中的项目?
- php - 嵌套语句的状态更新部分工作
- reactjs - reducer 中的函数影响了错误的状态属性。我知道为什么,但我不知道该怎么办
- c++ - MFC 控件上的文本 - 日文等 Unicode 字符被截断
- javascript - 在同一页面上处理表单提交 Spring Boot