Swift:UITableViewCell内嵌WKWebView每次显示重复加载问题
Hey there, let's break down how to fix your WKWebView-in-UITableViewCell issues step by step:
Core Problem: Repeated Loading & Scroll Lag
The root cause is that every time your video cell gets reused (when scrolling), you're triggering a fresh load(urlRequest) call in cellForRowAt. This forces the WKWebView to reload the YouTube embed every time the cell enters the viewport, which causes that noticeable lag.
Fix 1: Control WebView Loading in the Cell
Move the URL loading logic into your VideoTableViewCell and track whether the video has already been loaded to avoid redundant requests. Here's how to adjust the cell code:
import UIKit import WebKit class VideoTableViewCell: UITableViewCell, ViewModelConfigurable { typealias ViewModelType = ProductViewModel var viewModel: ViewModelType? @IBOutlet var videoWebKitView: WKWebView! private var loadedVideoId: String? // Tracks which video is already loaded func configureFrom(_ viewModel: ViewModelType) { self.viewModel = viewModel // Only reload if the video ID is new or hasn't been loaded yet guard let videoId = viewModel.embededYoutubeVideoId, videoId != loadedVideoId else { return } loadedVideoId = videoId if let embedUrl = URL(string: "https://www.youtube.com/embed/\(videoId)") { videoWebKitView.load(URLRequest(url: embedUrl)) } } override func awakeFromNib() { super.awakeFromNib() // Optimize WebView config to reduce overhead let webConfig = WKWebViewConfiguration() // Disable unnecessary gestures/features videoWebKitView.allowsBackForwardNavigationGestures = false videoWebKitView.configuration = webConfig } override func prepareForReuse() { super.prepareForReuse() // Reset state for reuse: stop loading and clear the loaded ID if needed videoWebKitView.stopLoading() // Uncomment below if you want the cell to reload the video when reused for a different item // loadedVideoId = nil } }
Then simplify your cellForRowAt to let the cell handle its own loading logic:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if product.productHasVideo { return tableView.deque(VideoTableViewCell.self, configureFrom: self.product, at: indexPath) } else { let cell = UITableViewCell() // Add any default styling here if needed return cell } }
Fix 2: Optimize TableView Scroll Performance
WKWebView is a heavy component, so reducing layout calculations can help with lag:
- Enable automatic row height with an estimated value in your view controller's
viewDidLoad:override func viewDidLoad() { super.viewDidLoad() tableView.estimatedRowHeight = 250 // Match your video cell's approximate height tableView.rowHeight = UITableView.automaticDimension } - Make sure your cell's auto-layout constraints are unambiguous and efficient (avoid nested scroll views if possible).
About the WF Logs
Those WF: _WebFilterIsActive logs are not errors—they're just system-level logs tracking iOS content filter settings. They have no connection to your scroll lag or repeated loading issues, so you can safely ignore them. Your plist changes weren't helping because this isn't a URL security problem.
内容的提问来源于stack exchange,提问作者Alex Ritter

