0

I am working on a cookbook app and I thought I had the knowledge to get the last bit up and running and I have run into a snag. I am trying to get my ingredients array to display on a text field and I cannot get it to work. I have done some research and I have tried a few different things and all of them come up with different errors. I have left them in the code but commented them out so maybe someone looking at it could see what I was doing wrong with what I had going. My code looks as follows for my view controller that will display the ingredients.

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var ingredients: UITextView!

@IBOutlet weak var directions: UITextView!


var recipe : Recipe!

// var ingredientText = String()

// let separator = (" / ")


override func viewDidLoad() {
    super.viewDidLoad()


    self.title = recipe.name

    directions.text = recipe.directions


 //       let ingredientsText = separator.join(recipe.ingredients)

   //     label.text = ingredientsText




  //  for ingredient: String in recipe.ingredients {

  //     ingredientsText.join("%@\n", ingredient)

 //  }




//    label.text = ingredientText

//   label.text = recipe.ingredients





//    var multiLineString = join("\n", recipe.ingredients)
//    label.text = multiLineString





}

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


}

this is the table VC

import UIKit

class tableVC: UITableViewController {

var recipes: [Recipe] = []

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Juicing Recipes"

    let recipe0: Recipe = Recipe()
    recipe0.name = "Number 1"
    recipe0.ingredients = ["Pasta","Pasta Sauce","Hamburger"]
    recipe0.directions = "Cook pasta to directions on box, brown hamburger, add sauce to hamburger, dump on pasta"
    recipes.append(recipe0)

    let recipe1: Recipe = Recipe()
    recipe1.name = "Number 2"
    recipe1.ingredients = ["all kinds of stuff","Then a dash of salt"]
    recipe1.directions = "enjoy"
    recipes.append(recipe1)


}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return recipes.count
}

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

    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    let recipe: Recipe = recipes[indexPath.row] 

    cell.textLabel?.text = recipe.name

    return cell
}

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

    let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!

    let destCon : ViewController = segue.destinationViewController as! ViewController

    destCon.recipe = recipes[indexPath.row] 



}
}

then the recipe class

class  Recipe: NSObject {
var name = String()
var ingredients = [String]()
var directions = String()
}

It's another one of those things I know it something simple that I am overlooking but I have been stumped on this for a while.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
k2smooth
  • 3
  • 1
  • I have implemented a few recipe apps. I used a table view for the ingredients and for the directions steps. – dasdom Jan 02 '16 at 20:41
  • One way to go around this is to create a custom `UITableCell` subclass, which you let contain the `Recipe` properties you've define abode (`name`, `ingredients`) (in essence, this subclass is your recipe class). Your custom class can then be directly associated with the table view cells. Another cell = another recipe. I recently wrote quite a detailed answer regarding communication between tableview and a VC, with a custom cell in the former, possibly that answer can help you out: http://stackoverflow.com/questions/34247239/global-variable-and-optional-binding-in-swift/34275632#34275632 – dfrib Jan 02 '16 at 20:46

0 Answers0