简体   繁体   中英

Swift 4.2 - How to call “indexPath.row” from “didSelectRowAtIndexPath” method in a tableView to another class?

I have two viewControllers. One has a tableView which has some cells from an array; And the other is a viewController which contains a webkit that I want to present some local HTML files. Also I defined the webKit page as tableView's next page. What I want is, when a user chooses a cell in the tableView, according to which cell is selected, it goes to webkit Page, and some parts of codes runs for user to show a specific HTML. In other words, I have 5 HTML files and 5 items in tableViewCells so if a user chooses for example the first item, the HTML1 shows to him, if he chooses the second cell, the HTML2 present for him etc.

I tried a lot of ways. but nothing happened. also tried to create instance object from the tableview class... tried also some same problem here ins StackOverFlow but not succeed

First Page:

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

the Second Page:

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
        }

You need to create a Int variable in WebKitController class as selectedRow where the selected row will get assigned from table view cell selection,

// 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
    }
}

I think you can pass your indexpath.row value in sender of your perfomSegue and your second view controller just use this

here is code

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "secondvc", sender: indexPath.row)
}

just send indexpath.row value in sender

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "secondvc"{
        let vc = segue.destination as! SecondViewController
        vc.row = sender as! Int
    }
}

As an alternative approach

I would store not a simple array but struct of name and bool is selected or not

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

Then in didSelectAtRow func will make selected the cell which tapped and use this info in prepare for segue func

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM