Swift - 在两个tableviews(2个viewcontrollers)之间传递数据

时间:2015-06-09 01:07:35

标签: swift tableview

表视图有问题,它只是viewcontrollers上的tableviews,我有意大利美食,墨西哥食物,英国美食在第一个tableview,无论我点击什么意大利美食随处可见“bolognese,milagnese,pizza”in mexican and english餐饮。为什么当我点击意大利食品时,我在第二张桌面视图中看不到前两排意大利食物?我还没有为那行输入墨西哥食物和英国食物阵列,因为我只测试意大利食物阵列。第二个表视图如何知道第一个表视图已被单击?使用didselectrowAtIndexPath方法对我来说很重要。非常感谢你!

firsttableview

secondtableview

以下是代码:

  

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.
}
*/

}

1 个答案:

答案 0 :(得分:2)

您可以在屏幕截图中看到所有数据都存在,问题是您的第二个表视图位于导航栏下方,您可以通过在第二个视图控制器的viewDidLoad中添加以下命令来修复它。这将在表格视图的顶部添加一些空间,因此当导航栏完成时将开始:

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

对于食物清单,你总是将它从一张桌子传递到另一张桌子,在示例中我没有在阵列中添加食物名称,请在测试之前这样做:

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
}

我希望能帮到你!