Igy
Igy

Reputation: 129

Swift - 2 tableviews and detailviewcontroller

have problem with 2 tableviews on 2 viewcontrollers and detailviewcontroller, everything is almost working ok, 2 tableviews working like a charm, but problem is in secondviewcontroller where i put array of images to segue to detailVC after each meal/food is clicked, i got same 3 images for mexican food, spanish food, italian, croatian and french. Images(15) does to correspond to 15 meals. Did i go wrong with putting array of images in second view controller? Thank you

1 2 3

ViewController

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{

let textForFirstTableView = ["Italian food", "Mexican food", "Croatian food", "Spanish food", "French food"]

let namesOfFood = [["Bolognese", "Milagnese","Pizza"],
                   ["Tortilla", "Chimichanga", "Paella"],
                   ["Burek od mesa","Grah", "Janjetina"],
                   ["Tapas", "Churros", "Flan"],
                   ["Buche de Noel", "Cherry Cake", "Onion Soup"]]

var ObjectNamesOfFood = [String]()

@IBOutlet weak var tableView: UITableView!
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 textForFirstTableView.count

}

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

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

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

    return cell

}

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

    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]

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

}

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

    let driver = segue.destinationViewController as! DrugiViewController

    var whatToPass = self.ObjectNamesOfFood

    driver.arrayToPass = whatToPass


}
}

SecondViewController

import UIKit

class DrugiViewController: UIViewController, UITableViewDelegate, UITableViewDataSource  {

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

var picturesForEachMeal = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15"]

var objectpicturesForEachMeal:String!


@IBOutlet weak var tableView2: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.tableView2.delegate = self
    self.tableView2.dataSource = self
}

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

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

    return arrayToPass.count
}

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

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

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

    return cell

}

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

     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row]

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

}

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

    let driver = segue.destinationViewController as! DetailViewController

    driver.picturesToRetrieve = objectpicturesForEachMeal

}
}

DetailViewController (displaying 15 images for 15 meals)

import UIKit

class DetailViewController: UIViewController {

@IBOutlet weak var backgroundImage: UIImageView!

@IBOutlet weak var slika: UIImageView!

var picturesToRetrieve:String!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    var connection = self.picturesToRetrieve
    self.slika.image = UIImage(named: connection)

}

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

Upvotes: 0

Views: 1576

Answers (2)

Özgür Ersil
Özgür Ersil

Reputation: 7013

In View Controller store the selected index and pass it to DrugiViewController

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.selectedKitchen = indexPath.row 
    self.performSegueWithIdentifier("Segue", sender: self)
}

In DrugiViewController calculate each food image index

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     var totalIndex = (indexPath.row) + (selectedKitchen * 3)
     self.objectpicturesForEachMeal = self.picturesForEachMeal[totalIndex]
     self.performSegueWithIdentifier("toDetailViewController", sender: self)
}

that's it

Upvotes: 1

Icaro
Icaro

Reputation: 14845

I must say this is not the best approach if you was starting an app from begining you should in your case use an dictionary of array that contain a key country and as result an array of dictionary dish and photo in the first view controller. In the second view controller you should have an array of dictionary that contain the dish and photo. In the third view just a simple dictionary that contain a dictionary with dish and photo.

But how you almost finish the easier solution for you is to pass the value from the first table to the second, so lets say if the user selected row 2 you pass and keep that values.

Now in the second table if the user selected row 3 you multiply the number of the row the user selected (3) by the number of the first row select in table view one (2) and you know you have to display the picture number 6 in your final array.

I hope this helps you, I am sure now that you know the idea you will be able to finish your project. Best of luck!

First view controller:

var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.ObjectNamesOfFood = self.namesOfFood[indexPath.row]
    self.rowSelected = indexPath.row
    self.performSegueWithIdentifier("Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let driver = segue.destinationViewController as! DrugiViewController
    var whatToPass = self.ObjectNamesOfFood
    driver.arrayToPass = whatToPass
    driver.rowSelected = rowSelected
}

Second view controller:

var rowSelected:Int?
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     self.objectpicturesForEachMeal = self.picturesForEachMeal[indexPath.row * self.rowSelected]
     self.performSegueWithIdentifier("toDetailViewController", sender: self)
}

This is the basic idea, sorry I couldn't test it as I am in a Windows computer now, but should just work

Upvotes: 1

Related Questions