Eu copiei o código do projeto eggplant e mudei pra ficar com meus alimentos e agora estou com muitos erros. Por favor me ajudei. Grata,
Veronica
//
// ViewController.swift
// Pizza
//
// Created by Veronica Segal on 25/09/17.
// Copyright © 2017 Veronica Segal. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet var nameField : UITextField?
@IBOutlet var happinessField : UITextField?
var delegate : AddAMealDelegate?
var selected = Array<Item>()
var items = [
Item(name: "Chocolate", calories: 140),
Item(name: "Brownie", calories: 100),
Item(name: "Coco", calories: 150),
Item(name:"Muffin", calories:80),
Item(name: "Caju", calories: 240),
Item(name: "Cacau", calories: 500),
Item(name: "Sorvete", calories: 800)]
}
override func viewDidLoad() {
let newItemButton = UIButtonItem (title: "new item",style: UIBarButtonStyle.plain, target: self, action: #selector("showNewItem"))
navigationItem.rightBarButtonItem = newItemButton
}
func showNewItem() {
print("New item...")
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
if(cell.accessoryType == UITableViewCellAccessoryType.none) {
cell.accessoryType = UITableViewCellAccessoryType.checkmark
let item = items[indexPath.row]
selected.append(item)
} else {
cell.accessoryType = UITableViewCellAccessoryType.none
let item = items[indexPath.row]
if let position = selected.index(of: item) {
selected.remove(at: position)
}
}
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = indexPath.row
let item = items[row]
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: nil)
cell.textLabel!.text = item.name
return cell
}
@IBAction func add() {
if(nameField == nil || happinessField == nil) {
return
}
let name:String = nameField!.text!
if let happiness = Int(happinessField!.text!) {
let meal = Meal(name: name, happiness: happiness, items: selected)
print("eaten \(meal.name) with happiness \(meal.happiness) with \(meal.items)!")
if(delegate == nil) {
return
}
delegate!.add(meal)
if let navigation = navigationController {
navigation.popViewController(animated: true)
}
}
}
}