Swift3: TextField events in TableViewCell

0

Cordial greeting. I work in an application which has a list of elements, each item in the list is composed of a custom cell (TableViewCell) which contains 1 textfield and several labels. I try to capture the value change event of the textfield in the cell. So far I have this code but I can not capture the event ... Any ideas?

TableViewCell

class TableViewCell: UITableViewCell, UITextFieldDelegate{

    @IBOutlet weak var imagen: UIImageView!
    @IBOutlet weak var producto: UILabel!
    @IBOutlet weak var precio: UILabel!
    @IBOutlet weak var cantidad: UITextField?
    @IBOutlet weak var stock: UILabel!

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        cantidad?.delegate = self
        self.contentView.addSubview(cantidad!)
    }

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

    @IBAction func txtCantidad_onChange(_ sender: Any) {
        if cantidad?.text != "0" {
           print("Cambio con valor != a 0")
        }else{
            print("Cambio con valor 0")
        }
    }
}

The code shown above is just an example, but I really want to run the entire code of the didSelectRowAt method of the tableView

The start code of the table is hosted in a separate ViewController

ViewController

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let celda = Bundle.main.loadNibNamed("ProductoViewCell", owner: self, options: nil)?.first as! ProductoViewCell
            let aux = arrProductos[indexPath.row] as! Producto

            let dataDecoded:NSData = NSData(base64Encoded: aux.imagen, options: NSData.Base64DecodingOptions(rawValue: 0))!
            let decodedimage:UIImage = UIImage(data: dataDecoded as Data)!
            celda.imagen.image = decodedimage
            celda.producto.text = aux.nombre
            celda.precio.text = "Precio: $\(Formato().Double2String(valor: aux.precioVenta))"
            celda.stock.text = "En stock \((arrProductos[indexPath.row] as! Producto).stock)"
            return celda
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let celda = tableView.cellForRow(at: indexPath)
            let aux = arrProductos[indexPath.row] as! Producto

            if (celda?.isSelected)! {
                if  (celda as! ProductoViewCell).cantidad?.text != "" {
                    if ((celda as! ProductoViewCell).cantidad?.text?.isInt)! && (celda as! ProductoViewCell).cantidad?.text != "0"{
                        if Int(((celda as! ProductoViewCell).cantidad?.text)!)! <= aux.stock
                        {
                            let nuevo: PedidoDetalle = PedidoDetalle()
                            nuevo.idProducto = aux.id;
                            nuevo.precioUnitario = aux.precioVenta
                            nuevo.cantidad = Int(((celda as! ProductoViewCell).cantidad?.text)!)!
                            nuevo.precioSubtotal = (nuevo.precioUnitario * Double(nuevo.cantidad))
                            nuevo.idParametrizacion = 1
                            arrProductosDetalle.add(nuevo)
                            lstPedidoDetalle.reloadData()
                            lblTotal.text = Formato().Double2String(valor: calcularTotal())
                            celda?.accessoryType = .checkmark
                        }else{
                            celda?.isSelected = false
                            Mensaje().mensaje(controller: self,texto: "El stock no es suficiente")
                        }
                    }else{
                        celda?.isSelected = false
                        (celda as! ProductoViewCell).cantidad?.text = ""
                        Mensaje().mensaje(controller: self, texto: "Tipo de dato no valido o valor no valido")
                    }
                }else{
                celda?.isSelected = false
                Mensaje().mensaje(controller: self,texto: "Primero defina la cantidad de este producto")}
            }else{

            }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if tableView == lstPedidos{
            let celda = tableView.cellForRow(at: indexPath)
            let aux = arrProductos[indexPath.row] as! Producto
            let nuevo: PedidoDetalle = PedidoDetalle()
            nuevo.idProducto = aux.id;
            nuevo.precioUnitario = aux.precioVenta
            nuevo.cantidad = Int(((celda as! ProductoViewCell).cantidad?.text)!)!
            nuevo.precioSubtotal = (nuevo.precioUnitario * Double(nuevo.cantidad))
            //arrPedidoProducto.add(nuevo)
            for item in arrProductosDetalle
            {
                let aux = item as! PedidoDetalle
                if(aux.idProducto == nuevo.idProducto && aux.cantidad == nuevo.cantidad)
                {
                    arrProductosDetalle.remove(aux)
                }
            }
            //arrProductosDetalle.removeObject(identicalTo: nuevo)
            (celda as! ProductoViewCell).cantidad?.text = ""
            celda?.accessoryType = .none
            lstPedidoDetalle.reloadData()
            lblTotal.text = Formato().Double2String(valor: calcularTotal())

        }
    }
    
asked by Kevtho 11.09.2017 в 22:14
source

1 answer

0

Sure there are more ways to do it but this is what I would do.

First remove the txtChange_onChange function from the class in the cell and put it in the viewController

Second add the target to the UITextField when each cell is created and add a corresponding tag to the line of the cell (row) to facilitate work later

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let celda = Bundle.main.loadNibNamed("ProductoViewCell", owner: self, options: nil)?.first as! ProductoViewCell
        let aux = arrProductos[indexPath.row] as! Producto

        let dataDecoded:NSData = NSData(base64Encoded: aux.imagen, options: NSData.Base64DecodingOptions(rawValue: 0))!
        let decodedimage:UIImage = UIImage(data: dataDecoded as Data)!
        celda.imagen.image = decodedimage
        celda.producto.text = aux.nombre
        celda.precio.text = "Precio: $\(Formato().Double2String(valor: aux.precioVenta))"
        celda.stock.text = "En stock \(aux.stock)"
        celda.cantidad.tag = indexPath.row
        celda.cantidad.addTarget(self, action: #selector(txtCantidad_onChange(_:)), for: .editingChanged)
        return celda
}

Third, modify the function txt Quantity_onChange so that it modifies the corresponding Product object (we know what it is thanks to the tag we added before). When dealing with objects, what you modify in aux will affect the original object in the list.

func txtCantidad_onChange(_ textFieldCantidad: UITextField) {
    let aux = arrProductos[textFieldCantidad.tag] as! Producto
    if textFieldCantidad.text != "0" {
       print("Cambio con valor != a 0")
       //aux.cantidad = textFieldCantidad.text
    }else{
        print("Cambio con valor 0")
    }
}

In the didSelectRow function you can avoid writing celda as! ProductoViewCell many times if you put it at the beginning

let celda = tableView.cellForRow(at: indexPath) as! ProductoViewCell
    
answered by 12.09.2017 / 14:11
source