How to obtain the model of a UITableViewCell

0

I would like to know if someone has a way to get the value of a class UITableViewCell .

class EncuestaRespuestaC: UITableViewCell {
    @IBOutlet weak var lblDescription: UILabel!
    @IBOutlet weak var ivStart01: UIButton!
    @IBOutlet weak var ivStart02: UIButton!
    @IBOutlet weak var ivStart03: UIButton!
    @IBOutlet weak var ivStart04: UIButton!
    @IBOutlet weak var ivStart05: UIButton!
    private var starts : [UIButton]!
    public var position = 0

    override func awakeFromNib() {
        super.awakeFromNib()

        initView()
    }

    private func initView() {
        starts = [ivStart01, ivStart02, ivStart03, ivStart04, ivStart05]
        for start in starts {
            start.addTarget(self, action: #selector(onClick(_:)), for: .touchUpInside)
        }
    }

    @objc private func onClick( _ button: UIButton) {
        position = button.tag
        setStart(position: position)
        position += 1
    }

    private func setStart(position: Int) {
        for (index, start) in starts.enumerated() {
            let color = index <= position ? Colors.Color.PURPLE_BUTTON : Colors.Color.GRIS_BUTTON
            setColorFilter(button: start, imageName: "ic_star", color: color)
        }
    }
}

// Obs Each of my 5 buttons has a tag (1,2,3,4,5)

I want to notify my models

my controller

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let pregunta = preguntas[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: ID_CELL, for: indexPath) as! EncuestaRespuestaC
        cell.lblDescription.text = pregunta.nomPregunta
        pregunta.respuesta = cell.position

        AnimationsUtil.animationTableCell(cell: cell)
        cell.selectionStyle = .none

        return cell
    }

my model is

class EncuestaRespuestaBE: NSObject {
    internal var idPregunta: Int? = 0 {
        didSet { idPregunta = idPregunta ?? 0 }
    }

    internal var nomPregunta: String? = "" {
        didSet { nomPregunta = nomPregunta ?? "" }
    }

    internal var respuesta: Int? = 0 {
        didSet { respuesta = respuesta ?? 0 }
    }

    override var description: String {
        return "\(EncuestaRespuestaBE.self){ idPregunta: \(idPregunta!), nomPregunta: \(nomPregunta!), respuesta: \(respuesta!) }"
    }
}

My intention is to notify the model inside the cell, and when to call the list of surveys. You should get them with the changes of the buttons.

    
asked by marlonpya 13.07.2017 в 02:37
source

3 answers

2

I'm not sure what you ask. What I understand is that when you press one of the buttons in a cell, access the corresponding object and keep the button pressed. Following the way I do it would change some things

I would change the cellForRowAt function of the controller so that the setStart takes place when loading each cell (otherwise it may have problems due to the reuse of cells) and would pass here the onClick function of the buttons to access the indexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let pregunta = preguntas[indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: ID_CELL, for: indexPath) as! EncuestaRespuestaC
    cell.lblDescription.text = pregunta.nomPregunta
    for start in cell.starts {
        start.addTarget(self, action: #selector(onClick(_:)), for: .touchUpInside)
    }
    cell.setStart(position: pregunta.position)

    AnimationsUtil.animationTableCell(cell: cell)
    cell.selectionStyle = .none

    return cell
}

@objc private func onClick( _ button: UIButton) {
    position = button.tag
    setStart(position: position)

    let view = button.superview //content view de la celda
    let cell = view?.superview as! EncuestaRespuestaC
    let indexPath = tableView.indexPath(for: cell)

    preguntas[indexPath.row].respuesta = position
    tableView.reloadData()
}

Due to the previous changes, the cell model would look like this:

class EncuestaRespuestaC: UITableViewCell {
    @IBOutlet weak var lblDescription: UILabel!
    @IBOutlet weak var ivStart01: UIButton!
    @IBOutlet weak var ivStart02: UIButton!
    @IBOutlet weak var ivStart03: UIButton!
    @IBOutlet weak var ivStart04: UIButton!
    @IBOutlet weak var ivStart05: UIButton!
    private var starts : [UIButton]!
    public var position = 0

    override func awakeFromNib() {
        super.awakeFromNib()
        starts = [ivStart01, ivStart02, ivStart03, ivStart04, ivStart05]
    }

    func setStart(position: Int) {
        for (index, start) in starts.enumerated() {
            let color = index <= position ? Colors.Color.PURPLE_BUTTON : Colors.Color.GRIS_BUTTON
            setColorFilter(button: start, imageName: "ic_star", color: color)
        }
    }
}

I hope to have explained correctly and to help you for this occasion and future similar problems

    
answered by 14.07.2017 / 08:44
source
1

I am not clear about your requirement, but to obtain the selected cell you can use the didSelectRowAtIndexPath method.

    
answered by 13.07.2017 в 23:42
1

This answer is for Yoana Ugarte García. Thank you very much you helped me too much to understand. Look I solved it like that since it's the order of my UIView.

@objc private func onClick(_ button: UIButton) {
        //stackView, view, view, view, cell
        let position = button.tag
        let view = button.superview as! UIStackView
        let cell = view.superview?.superview?.superview?.superview as! EncuestaRespuestaC
        let indexPath = tableView.indexPath(for: cell)
        preguntas[indexPath!.row].respuesta = (position + 1)

        for (index, start) in cell.starts.enumerated() {
            let color = index <= position ? Colors.Color.PURPLE_BUTTON : Colors.Color.GRIS_BUTTON
            setColorFilter(button: start, imageName: "ic_star", color: color)
        }
    }

This is my hierarchy ..

    
answered by 14.07.2017 в 23:27