Swift - 在两个表视图之间传递数据(2 个视图控制器)

Swift - passing data between two tableviews (2 viewcontrollers)

table 视图有问题,它只是视图控制器上的 table 视图,我在第一个 table 视图中有意大利菜、墨西哥菜、英国菜,无论我点击什么在墨西哥和英国食物中到处都有意大利食物 "bolognese, milagnese, pizza"。为什么当我单击意大利菜时,我在第二个 table 视图中看不到前两行意大利菜?我还没有为该行输入墨西哥食物和英国食物数组,因为我只是测试意大利食物数组。第二个 table 视图如何知道第一个 table 视图已被单击?对我来说重要的是经常使用 didselectrowAtIndexPath method.Thank 你!


代码如下:

ViewController (firsttableview)

import UIKit

    class ViewController: UIViewController, UITableViewDelegate,    UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!
let namesForCell = ["Italian Cousine", "Mexican Food", "English kitchen"]
let textWhenRowIsClicked = ["Bolognese", "Milagnese", "Ravioli"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.delegate = self
    self.tableView.dataSource = self
}

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

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

    return namesForCell.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Basiccell") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {


    self.performSegueWithIdentifier("toTableView", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let detailVC = segue.destinationViewController as! secondTableView

    var whatToPass = self.textWhenRowIsClicked

    detailVC.namesForCell2 = whatToPass



}

}

Secondtableview (viewcontroller)

import UIKit

   class secondTableView: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var tableView1: UITableView!

var namesForCell2:[String] = [String]()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.tableView1.delegate = self
    self.tableView1.dataSource = self

}

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

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

    return namesForCell2.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("Basiccell2") as! UITableViewCell

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

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    //self.talijanskaJela = self.tekstKadSeKlikneRow[indexPath.row] as String

   // self.performSegueWithIdentifier("toTableView", sender: self)

}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

你可以在屏幕截图中看到所有数据都在那里,问题是你的第二个 table 视图在你的导航栏下方,你可以通过在你的 viewDidLoad 中添加下面的命令来修复它第二个视图控制器。这将在 table 视图的顶部添加一些 space,因此将在导航栏完成时开始:

table1.ScrollIndicatorInsets = new UIEdgeInsets(64, 0, 0, 0);

对于食物列表,您总是将相同的食物从一个 table 传递给另一个,在示例中我没有在数组中添加食物名称,请在测试前添加:

let namesForCell = ["Italian Cousine", "Mexican Food", "English kitchen"]
let textWhenRowIsClicked = [["Bolognese", "Milagnese", "Ravioli"],[mexican array], [Englis Array]] //bidimentional array to keep all foods from all kitchens in one array
var listOfFoodToPass = [String]() //Keep the array to be pass

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.listOfFoodToPass = textWhenRowIsClicked[indexPath.row] //put the correct list of the food in the array to be passed
    self.performSegueWithIdentifier("toTableView", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let detailVC = segue.destinationViewController as! secondTableView
    var whatToPass = self.listOfFoodToPass //pass the array to the new table with the correct list of food
    detailVC.namesForCell2 = whatToPass
}

希望对你有帮助!