1
resposta

Problema com os checkmarks

O Table View esconde o botão de add na tela de inserir alimentos e os checkmarks não funcionam. Tem um tópico sobre esse mesmo assunto, já fechado, mas sem a resolução. O meu código do ViewController completo é:

//
//  ViewController.swift
//  eggplant-brownie
//
//  Created by Andriu Felipe Coelho on 23/02/19.
//  Copyright © 2019 Alura. All rights reserved.
//

import UIKit

protocol AdicionaRefeicaoDelegate {
    func add(_ refeicao: Refeicao)
}

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    // MARK: - Atributos

    var delegate: AdicionaRefeicaoDelegate?
    var itens: [String] = ["Molho de tomate", "Queijo", "Molho apimentado", "Manjericão"]

    // MARK: - IBOutlets

    @IBOutlet var nomeTextField: UITextField?
    @IBOutlet weak var felicidadeTextField: UITextField?

    // MARK: - UITableViewDataSource

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let celula = UITableViewCell(style: .default, reuseIdentifier: nil)

        let linhaDaTabela = indexPath.row
        let item = itens[linhaDaTabela]

        celula.textLabel?.text = item

        return celula
    }

    // MARK: - UITableViewDelegate

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let celula = tableView.cellForRow(at: indexPath) else { return }
        celula.accessoryType = .checkmark

        if celula.accessoryType == .none {
            celula.accessoryType = .checkmark
        }
        else {
            celula.accessoryType = .none
        }
    }

    // MARK: - IBActions

    @IBAction func adicionar(_ sender: Any) {

        guard let nomeDaRefeicao = nomeTextField?.text else {
            return
        }

        guard let felicidadeDaRefeicao = felicidadeTextField?.text, let felicidade = Int(felicidadeDaRefeicao) else {
            return
        }

        let refeicao = Refeicao(nome: nomeDaRefeicao, felicidade: felicidade)

        print("comi \(refeicao.nome) e fiquei com felicidade: \(refeicao.felicidade)")

        delegate?.add(refeicao)
        navigationController?.popViewController(animated: true)
    }
}

1 resposta

Fala Gabriel, de boa ?

Cara vi que tu abriu essa dúvida já tem um tempinho, será que já conseguiu resolve-la ?

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software