-1

我的项目中有两个表视图,任何人都可以帮助我获取第一个表视图的索引 path.row,这是要在第二个表视图中使用的主表,它是主表的子表。

这是我的 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){}


    })



     }
      }

   }

这是我的 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
    }


    }




     }
4

2 回答 2

0

也许使用元组更容易理解。SubWomenView.subWomenArray =(index.row , self.womenArray)

在子表视图中,

var subWomenArray : (Int,[women])

然后 subWomenArray.1 指的是你的 [women]

于 2017-01-01T01:18:28.800 回答
0

您可以在您的子表视图上创建一个属性,并将 indexPath.row 的值分配给它,就像您在下面的行中分配数组一样:

SubWomenView.subWomenArray = self.womenArray

于 2016-12-31T22:59:58.860 回答