将数据从tableview传递到另一个tableview

时间:2020-06-11 07:54:14

标签: ios swift xcode uitableview segue

我是迅速入门的人,我真的很想解决此问题。我正在尝试的是将数据从tableview传递到另一个视图,但是当我运行代码时,它只会显示空白的tableview。这是我在源tableviewcontroller中的代码:

import UIKit

class C1TableViewController: UITableViewController {

var categorisePagePOneName = ["CLOTHING", "SHOES", "ACCESSORIES", "THEME"]
var selectedIndex = Int()



override func viewDidLoad() {
    super.viewDidLoad()

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

    return 1
}

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

    return categorisePagePOneName.count
}


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

    let cellIdt = "Conecell"
  let cell = tableView.dequeueReusableCell(withIdentifier: cellIdt, for: indexPath) as! C1TableViewCell

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


    // Configure the cell...

    return cell
}


func laodForNextPage(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, segue: UIStoryboardSegue, sender: AnyObject?) {
    performSegue(withIdentifier: "connect2", sender: self)
    self.selectedIndex = indexPath.row
    if segue.identifier == "connect2" {
        let vc = segue.destination as! C2TableViewController
        vc.firstRow = categorisePagePOneName[self.selectedIndex]

    }
}

}

然后这是我在另一个tableViewController中的代码:

import UIKit

class C2TableViewController: UITableViewController {

var firstRow: String!


var secondArray: [String] = []


var clothingCat = ["ALL CLOTHING", "THEME", "JACKET", "T-SHIRT", "SHIRT", "SWEATSHIRT", "HOODIES", "PANTS", "JEANS", "SHORTS", "KNITWEAR", "VESTS"]

var shoesCat = ["ALL SHOES", "THEME", "BOOTS", "SANDALS", "SNEAKERS"]

var asscessoriesCat = ["ALL ASSCESSORIES", "THEME", "HAT","BAGS", "JEWELLERY", "EYEWEAR", "WATCHES", "WALLETS", "SOCKS", "UNDERWEAR"]

var themeCat = ["VINTAGE", "KOREA","JAPAN", "SPORTY"]


override func viewDidLoad() {
    super.viewDidLoad()
    }

// 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 secondArray.count
}


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


    let cell = tableView.dequeueReusableCell(withIdentifier: "C2", for: indexPath) as! C2TableViewCell

    // Configure the cell...

    switch firstRow {
     case "CLOTHING":
         secondArray = clothingCat

     case "SHOES":
         secondArray = shoesCat

     case "ASSCESSORIES":
     secondArray = asscessoriesCat

     case "THEME":
         secondArray = themeCat

     default:
         break
     }
    cell.textLabel?.text = secondArray[indexPath.row]


    return cell
}

}

我不确定在这种情况下是否正确使用了开关

2 个答案:

答案 0 :(得分:1)

替换(自定义方法laodForNextPage仍然没有用)

func laodForNextPage(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath, segue: UIStoryboardSegue, sender: AnyObject?) {
    performSegue(withIdentifier: "connect2", sender: self)
    self.selectedIndex = indexPath.row
    if segue.identifier == "connect2" {
        let vc = segue.destination as! C2TableViewController
        vc.firstRow = categorisePagePOneName[self.selectedIndex]

    }
}

使用

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    performSegue(withIdentifier: "connect2", sender: indexPath)
}

并实施

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "connect2" {
        let indexPath = sender as! IndexPath
        let vc = segue.destination as! C2TableViewController
        vc.firstRow = categorisePagePOneName[indexPath.row]
    }
}

您可以删除selectedIndex

答案 1 :(得分:0)

您需要在发送者为indexPath时添加此功能

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

}
相关问题