Swift - passing data between two tableviews (2 viewcontrollers)

2.1k views Asked by At

Have problem with table views, it's just tableviews on viewcontrollers, i have italian food, mexican food, english food in first tableview, and whatever i click i got italian food everywhere "bolognese, milagnese, pizza" in mexican and in english food. And why when i click italian food, i can't see first two rows of italian food in second tableview? I didn't yet typed mexican food and english food arrays for that rows, because i'm testing just italian food array. How will second table view know that first table view has been clicked? It's important for me to use didselectrowAtIndexPath method.Thank you a lot!

firsttableview


secondtableview

Here are the codes:

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

There are 1 answers

6
Icaro On BEST ANSWER

You can see in the screenshot that all the data is there, the problem is that your second table view is going under your navigation bar, you can fix it by adding the command below in your viewDidLoad in the second view controller. this will add some space in the top of the table view so is will start when the navigation bar finish:

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

For the list of food you are always passing the same away from one table to another, in the example I didn't add food names in the array please do that before test:

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
}

I hope that helps you!