Solucionado (ver solução)
Solucionado
(ver solução)
7
respostas

Checkmark não funciona

Olá, eu tentei fazer como sugere a aula, porém não obtive êxito ao rodar o programa, ele não aparece o "check" quando seleciono algum item na tabela. Segue o código abaixo:

   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 ]
    var cell = UITableViewCell(style:UITableViewCellStyle.default,reuseIdentifier: nil)
        cell.textLabel?.text = item.name
        return cell
    }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        if cell == nil {
            return
        }
        cell!.accessoryType = UITableViewCellAccessoryType.checkmark
    }
7 respostas

Bom dia Mateus,

Não estou vendo nenhum erro "a olho nu". Pode colocar um print para ver se o método esta sendo chamado?

[]

http://imagizer.imageshack.us/a/img921/5386/WxStTe.png

http://imagizer.imageshack.us/a/img924/7412/4ubZnJ.png

Código do ViewContoller





import UIKit
protocol AddMealDelegate{
    func add(meal:Meal)
}

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var nameField: UITextField?
    @IBOutlet var happinessField: UITextField?
    var delegate:AddMealDelegate?

    var items = [
        Item(name: "Eggplant Brownie", calories: 10),
        Item(name: "Zuccchini muffin", calories: 90),
        Item(name: "Salsicha", calories: 15),
        Item(name: "Pizza", calories: 100),
        Item(name: "Lazanha", calories: 800),
                ]

    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
    }

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        if cell == nil {
            return
        }
        cell!.accessoryType = UITableViewCellAccessoryType.checkmark
    }

    @IBAction func add() {

        if nameField == nil || happinessField == nil{
            return
        }


        let name:String = nameField!.text!
        let happiness:Int = Int(happinessField!.text!)!




        let meal = Meal(name: name,happiness: happiness)
        print("eaten: \(meal.name) \(meal.happiness)!")

        if (delegate == nil){
            return
        }

        delegate!.add(meal: meal)

        if let navigation = self.navigationController{
            navigation.popViewController(animated: true)
        }
    }


}

Código MealsTableViewController

import UIKit

class MealsTableViewController: UITableViewController, AddMealDelegate {

    var meals = [Meal(name: "eggplant-brownie", happiness: 5),
                 Meal(name: "zucchini muffin", happiness: 3)
                ]

    func add(meal: Meal) {
        meals.append(meal)
        tableView.reloadData()
    }



    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if(segue.identifier == "addMeal") {
            let view = segue.destination as! ViewController
            view.delegate = self
        }
    }


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


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let row = indexPath.row
        let meal = meals[ row ]
        let cell = UITableViewCell(style: UITableViewCellStyle.default,
                                   reuseIdentifier: nil)
        cell.textLabel?.text = meal.name
        return cell
    }


}

Bom dia Mateus,

Desculpe, quis dizer adicionar a linha: print("entrei enesse metodo"), e print("tenho uma celula") etc, como uma forma simples de logar as informações para detectar se o problema está no binding do evento ou do datasource?

[]

Olá Guilherme, segue abaixo os links :), me desculpe pelos erros!

Imagem1

http://imageshack.com/a/img924/285/9QXV31.png

Imagem 2

http://imagizer.imageshack.us/a/img923/7518/Bj76fY.png

Imagem 3

http://imagizer.imageshack.us/a/img922/591/AxewJV.png

Imagem 4

http://imageshack.com/a/img924/9730/h9uIjE.png

Imagem 5

http://imagizer.imageshack.us/a/img921/5791/GHLKSp.png

Bom dia Mateus,

Não estou achando nada de errado no código. Você chegou a criar o projeto isolado de tabela dinâmica? Se sim, o que acha de naquele projeto adicionar o método de long press e fazer o suporte ao checklist para confirmar se funciona? Se funcionar, sabemos que é algo de configuração mesmo que está faltando e não estamos vendo.

[]

solução!

Olá Guilherme, Boa Tarde Eu criei sim um projeto a parte. Testei apenas o nosso método de "Check" e funcionou. Eu notei que o "Dispositivo" que eu estava usando no projeto da tabela dinâmica era o iPhone 7 e no Projeto eggplant-brownie eu estava usando o Iphone 5. Testei o iPhone 5 e 5s e funcionou apenas a partir do iPhone 6 em diante. Como você mesmo já tinha dito o problema não é o código e sim na escolha de dispositivo. Muitissimo obrigado pela ajuda e me desculpe os erros :)

Que ótimo Mateus, o importante é ter encontrado o motivo!

[]s

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