1
resposta

Add New Item não adiciona na lista

Não sei o que é, mas quando clico em New Item o item não aparece na lista... View Controller class ViewController: UIViewController , UITableViewDataSource,UITableViewDelegate, AddAnItemDelegate{

    @IBOutlet var nameField : UITextField?
    @IBOutlet var happinessField : UITextField?
    var delegate : MealsTableViewController?
    var selected = Array<Item>()
    var itens = [Item(name: "Eggplant", calorie: 10),
             Item(name: "Brownie", calorie: 10),
             Item(name: "Zucchini", calorie: 10),
             Item(name: "Muffin", calorie: 10),
             Item(name: "Coconut oil", calorie: 500),
             Item(name: "Chocolate frosting", calorie: 1000),
             Item(name: "Chocolate chip", calorie: 1000)]
    @IBOutlet var tableView: UITableView?

func add(_ item: Item) {
    itens.append(item)
    if let table = tableView {
        table.reloadData()
    }
}

@objc func showNewItem() {
    let newItem = NewItemViewController(delegate: self)
    if let navigation = navigationController{
        navigation.pushViewController(newItem, animated: true)
    }
}

override func viewDidLoad() {
    let newItemButton = UIBarButtonItem(title: "New Item",
                                        style: UIBarButtonItem.Style.plain,
                                        target: self, action:#selector(showNewItem))
    navigationItem.rightBarButtonItem = newItemButton

}    

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(itens[0]==itens[1])
    if let cell = tableView.cellForRow(at: indexPath){
        if (cell.accessoryType == UITableViewCell.AccessoryType.none){
            cell.accessoryType = UITableViewCell.AccessoryType.checkmark
            let item = itens[indexPath.row]
            selected.append(item)
        }else {
            cell.accessoryType = UITableViewCell.AccessoryType.none
            let item = itens[indexPath.row]
            if let position = selected.firstIndex(of: item){
                selected.remove(at: position)
            }
        }
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection: Int) -> Int {
    return itens.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let row = indexPath.row
    let item = itens[row]
    let cell = UITableViewCell(style: UITableViewCell.CellStyle.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)
            meal.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)
        }
    }
}

}

NewItemViewController class NewItemViewController: UIViewController { @IBOutlet var nameField: UITextField? @IBOutlet var calorieField: UITextField? var delegate: AddAnItemDelegate?

init(delegate: AddAnItemDelegate) {
    super.init(nibName: "NewItemViewController", bundle: nil)
    self.delegate = delegate
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}


@IBAction func addNewItem() {

    let name = nameField!.text
    let calories = Double(calorieField!.text!)

    if(name == nil || calories == nil || delegate == nil) {
        return
    }

    let item = Item(name: name!, calorie: calories!)
    delegate!.add(item)

    print("adding new item")
    if let navigation = navigationController {
        navigation.popViewController(animated: true)
    }
}

}

AddAnItemDelegate import Foundation

protocol AddAnItemDelegate {

func add(_ item: Item)

}

1 resposta

Oi Cristiane, tudo bem ?

Cara eu vi que sua dúvida é um pouco antiga, será que tu já resolveu ? Se sim pode compartilhar conosco ?