How can I get the index path.row of first table view to use it in the second table view using Swift3 , Xcode8

594 views Asked by At

I have two table views in my project , Can any one help me to get the index path.row of first table view which is the main table to use in the second table view which is the sub table of the main table.

this is My mainTableView.

    import UIKit

    class MainTableViewController: UITableViewController {


var  womenArray = [women]()
var data : [String] = []
let backendless = Backendless()

override func viewDidLoad() {
    super.viewDidLoad()

    data = ["1","2","3","4","5"]

}

override func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return data.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)



    cell.textLabel?.text = data[indexPath.row]

    return cell
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.womenArray.removeAll()

    if (indexPath.row == 0) {

        let whereClause = "Desc = 'test'"
        let dataQuery = BackendlessDataQuery()
        dataQuery.queryOptions.pageSize=50
        dataQuery.whereClause = whereClause

        backendless.data.of(women.ofClass()).find(dataQuery,response: {(result: BackendlessCollection?) -> Void in


            let data = result?.getCurrentPage()

            for obj in data! as! [women] {

                self.womenArray.append(obj)

            }
let SubWomenView: WomenSubTableViewController = self.storyboard!.instantiateViewController(withIdentifier: "subwomen") as! WomenSubTableViewController

SubWomenView.subWomenArray = self.womenArray

self.navigationController?.pushViewController(SubWomenView, animated: true)



},

error: { (fault: Fault?) -> Void in

 print(fault)

 let alert = UIAlertController(title: "info", message:"يرجى الاتصال بالانترنيت", preferredStyle: .alert)

   alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in


     })

self.present(alert, animated: true){}


        })



    }else if indexPath.row == 1{

        let whereClause = "Desc = 'test2'"
        let dataQuery = BackendlessDataQuery()
        dataQuery.queryOptions.pageSize=50
        dataQuery.whereClause = whereClause

        backendless.data.of(women.ofClass()).find(dataQuery,response: {(result: BackendlessCollection?) -> Void in


            let data = result?.getCurrentPage()

            for obj in data! as! [women] {

                self.womenArray.append(obj)

            }
let SubWomenView: WomenSubTableViewController = self.storyboard!.instantiateViewController(withIdentifier: "subwomen") as! WomenSubTableViewController
SubWomenView.subWomenArray = self.womenArray
self.navigationController?.pushViewController(SubWomenView, animated: true)



            },

  error: { (fault: Fault?) -> Void in



let alert = UIAlertController(title: "info", message:"يرجى الاتصال بالانترنيت", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in


    })

self.present(alert, animated: true){}


    })



     }
      }

   }

and this is My SubTableView.

 import UIKit

 class SubTableViewController: UITableViewController {

var subWomenArray = [women]()


  let backendless = Backendless()

override func viewDidLoad() {
    super.viewDidLoad()



        }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return subWomenArray.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? WomenSubCell{

        let ImgURL = URL(string : self.subWomenArray[(indexPath as NSIndexPath).row].ImgURL!)

        cell.WomenImgView.sd_setImage(with: ImgURL)

        return cell

    }else{

        let cell = WomenSubCell()

        let ImgURL = URL(string : self.subWomenArray [(indexPath as NSIndexPath).row].ImgURL)

        cell.WomenImgView.sd_setImage(with: ImgURL)



        return cell
    }


    }




     }
2

There are 2 answers

1
Praveen Pendyala On

You can create a property on your Sub Table View and assign the value of indexPath.row to it as you are assigning the array in the below line:

SubWomenView.subWomenArray = self.womenArray

1
Eric On

maybe use tuple is easier to understand. SubWomenView.subWomenArray =(index.row , self.womenArray)

In subtableview,

var subWomenArray : (Int,[women])

Then subWomenArray.1 refer to your [women]