-1

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
    }


    }




     }
  • It Isn't clear what you are asking. Do you have both table view controllers embedded in a parent view controller and shown on the screen at the same time, or does tapping on one link to the second table view controller? If one leads to the next, how are you invoking the second view controller? You need to provide more information. – Duncan C Dec 31 '16 at 21:31
  • I'am using the if statement in My main table view to download data for every index path.row , Now I want to use these if statements in the sub table view instead of main table view to download data can you help? . – Zaid Mustafa Jan 01 '17 at 08:01
  • I still don't understand. Is the second table view a different screen? So is this a master/detail setup? – Duncan C Jan 01 '17 at 12:32
  • Yes it's different , and it's not master detail .The first table view a simple array of [1,2,3,4] and Iam downloading data from database in it but I view it in the second table ,so I want to get indexpath.row of first table to use it in the second table instead of using it in the first table . – Zaid Mustafa Jan 01 '17 at 13:49

2 Answers2

0

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

  • It doesn't work , I'am try to get the index path.row of first table view to use it in second table view , So I want to use the if statement in the second table instead of first table in the second table . – Zaid Mustafa Jan 01 '17 at 08:05
0

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]

Eric
  • 1
  • It doesn't work , I'am try to get the index path.row of first table view to use it in second table view , So I want to use the if statement instead of first table in the second table . – Zaid Mustafa Jan 01 '17 at 08:04