我有两个viewControllers。一个有一个tableView,其中有一个数组中的一些单元格。另一个是viewController,其中包含一个Webkit,我想展示一些本地HTML文件。另外,我将webKit页面定义为tableView的下一页。 我想要的是,当用户在tableView中选择一个单元格时,根据选择的单元格,它会转到webkit页面,并且运行部分代码以供用户显示特定的HTML。换句话说,我在tableViewCells中有5个HTML文件和5个项目 因此,例如,如果用户选择第一个项目,则HTML1会向他显示;如果选择第二个单元格,则会向他显示HTML2等。
我尝试了很多方法。但是什么也没发生。还尝试从tableview类创建实例对象... 在这里尝试了同样的问题ins StackOverFlow但没有成功
第一页:
var arr = ["masoud" , "hossein", "reza" , "shiva" , "fateme"]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("item selected is : \(indexPath.row)")
// if indexPath.row == 1 {
performSegue(withIdentifier: "TableSegue", sender: nil)
//}
tableView.deselectRow(at: indexPath, animated: true)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.destination.view, segue.destination, sender) {
case let (_, controller as WebKitController, indexPath as NSIndexPath):
controller.indexPath = indexPath
break
default:
break
}
}
第二页:
import WebKit
class WebKitController: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var myWebKit: WKWebView!
var reza : String = String()
var indexPath: NSIndexPath? {
didSet {
reza = (indexPath?.row.description)!
print(indexPath?.row as Any)
self.myWebKit.uiDelegate = self
self.myWebKit.navigationDelegate = self
}
}
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "reza", withExtension: "html", subdirectory: "ReZeynoo2")!
myWebKit.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
myWebKit.load(request)
self.myWebKit.uiDelegate = self
self.myWebKit.navigationDelegate = self
}
答案 0 :(得分:0)
您需要在Int
类中以WebKitController
创建一个selectedRow
变量,其中将从表视图单元格选择中分配所选行,
// MARK: - Table view delegates
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "TableSegue", sender: indexPath)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "TableSegue", let indexPath = sender as? IndexPath {
let webController = segue.destination as? WebKitController
webController.selectedRow = indexPath.row
}
}
答案 1 :(得分:0)
我认为您可以将indexpath.row
的值传递给perfomSegue
的发件人,而您的第二个视图控制器只需使用
这是代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "secondvc", sender: indexPath.row)
}
只需在发件人中发送indexpath.row
值
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "secondvc"{
let vc = segue.destination as! SecondViewController
vc.row = sender as! Int
}
}
答案 2 :(得分:0)
作为一种替代方法
我不会存储一个简单的数组,而是存储name结构和bool是否被选中
struct CellData {
let name: String!
let isSelected: Bool!
let index: Int!
init(name: String, isSelected: Bool, index: Int) {
self.name = name
self.isSelected = isSelected
self.index = index
}
}
然后在didSelectAtRow函数中,将选择被点击的单元格并使用此信息来准备segue函数
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.deselectAll()
self.cellItems[indexPath.row].isSelected = true
performSegue(withIdentifier: "TableSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch (segue.destination.view, segue.destination, sender) {
case let (_, controller as WebKitController, indexPath as NSIndexPath):
if let item = cellItems.first(where: {$0.isSelected == true}) {
controller.indexPath = item.index
}
break
default:
break
}
}
private func deselectAll() {
for item in cellItems {
item.isSelected = false
}
}