我拥有的是一个包含大约 5 个单元格的表格 View 。每个单元格都有不同的标题,我想要的是当用户单击某个单元格时,会弹出与他们单击的标题相关的某个 YouTube 视频(所有单元格都有不同的标题)。我正在使用 WKWebView,截至目前,只要单击一行,就会弹出一个 Youtube 视频,但无论单击哪一行/标题,它都是相同的 Youtube 视频。如何根据单击的行获得不同的视频?
这是包含单元格/标题的 ViewController 的代码,它移动到的下一个 View Controller 只是一个使用 WebKit 显示带有链接的 Youtube Vid 的文件。
import UIKit
class ViewController: UITableViewController {
var motivationalVideoOptions = ["Work Hard And Be Patient", "Put Yourself In A Position To Succeed", "Stop Judging Yourself", "Lose The Battle But Win The War", "Top 10 Rules For Success"]
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
title = "Choose a video"
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return motivationalVideoOptions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "VideoOptions", for: indexPath)
cell.textLabel?.text = motivationalVideoOptions[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "VideoPlay") as? VideoPlayViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
}
其他 View Controller 类:
import UIKit
import WebKit
class VideoPlayViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
var progressView: UIProgressView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://youtu.be/T_aFqEmL5JI")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
最佳答案
之所以只跳转到一个视频,是因为你将url设置为常量链接。在 url 中创建一个基于标题或带有 TableView 单元格数据的变量。我现在正在打电话,请耐心等待。
示例:
我触摸了这个带有链接子标题的单元格。我引用了 Web View 并将链接设置为 View 的子标题。然后,该东西将加载变量链接。这会给你你想要的结果
关于ios - 当使用 UITableView 选择一行时,如何显示不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49834142/