我正在制作一本食谱应用程序,我认为我已经掌握了最后一点运行的知识,而且我遇到了麻烦。我试图让我的成分数组显示在文本字段上,我无法让它工作。我做了一些研究,我尝试了一些不同的东西,所有这些都会产生不同的错误。我把它们留在了代码中,但是对它们进行了评论,所以也许看到它的人可能会看到我做错了什么。我的代码看起来如下,我的视图控制器将显示成分。
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.
}
}
这是表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]
}
}
然后是食谱类
class Recipe: NSObject {
var name = String()
var ingredients = [String]()
var directions = String()
}
这是另一个我知道的事情我觉得很简单,但是我已经被困了一段时间了。