1

I have a tableView with action buttons, one of them are hide until the user click the other button, I was looking how to do that and I found that I have to implement a delegate like the code below:

Class TableViewCell:

import UIKit
import FLAnimatedImage
 
protocol OnButtonsClickDelegate:AnyObject{
    func onBtnDownloadClick(cell: ListadoTableViewCell)
}
 
class ListadoTableViewCell: UITableViewCell {
    @IBOutlet weak var lblAnterior: UILabel!
    @IBOutlet weak var lblCompras: UILabel!
    @IBOutlet weak var lblDevolucion: UILabel!
    @IBOutlet weak var lblSaldo: UILabel!
    @IBOutlet weak var lblAbonos: UILabel!
    @IBOutlet weak var lblNuevo: UILabel!
    @IBOutlet weak var lblDiferido: UILabel!
    @IBOutlet weak var lblCliente: UILabel!
    @IBOutlet weak var lblNombreCliente: UILabel!
    @IBOutlet weak var spinner: FLAnimatedImageView!
    @IBOutlet weak var btnDowload: UIButton!
    @IBOutlet weak var btnShare: UIButton!
    
    var onButtonsClickDelegate : OnButtonsClickDelegate!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    @IBAction func onBtnDownloadClick(_ sender: AnyObject){
        onButtonsClickDelegate.onBtnDownloadClick(cell: self)
    }
 
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
 
        // Configure the view for the selected state
    }
 
}

Class Controller:

class ListadoController: NavigationViewController, UITableViewDelegate, UITableViewDataSource, RefreshScrollViewDelegate,OnButtonsClickDelegate {
 
    @IBOutlet weak var tableView: RefreshTableView!
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellListado") as! ListadoTableViewCell
        
        let r = data[indexPath.row]
      
        
        let compras = Functions.stringToFloat(str: r.compras)
        let comprasn = Functions.stringToFloat(str: r.comprasn)
        let abonos = Functions.stringToFloat(str: r.abonos)
        let diferido = Functions.stringToFloat(str: r.diferido)
        let saldomov = Functions.stringToFloat(str: r.saldomov)
        
        cell.lblAnterior.text = Functions.moneyFormat(n: saldomov - compras - comprasn)
        cell.lblCompras.text = Functions.moneyFormat(n: compras)
        cell.lblDevolucion.text = Functions.moneyFormat(n: 0.0)
        cell.lblSaldo.text = Functions.moneyFormat(n: saldomov - comprasn)
        cell.lblAbonos.text = Functions.moneyFormat(n: abonos) + ""
        cell.lblNuevo.text = Functions.moneyFormat(n: saldomov - comprasn - abonos) + ""
        cell.lblDiferido.text = Functions.moneyFormat(n: diferido) + ""
        cell.lblCliente.text = r.nombre.capitalized
        cell.lblNombreCliente.text = r.cvecte
        cell.onButtonsClickDelegate = self
        
        if indexPath.row == data.count - 1 {
            if (!last && !loading)  {
                loadData(page: currentPage)
            }
        }
        return cell
    }
    func onBtnDownloadClick(cell: ListadoTableViewCell) {
        cell.btnShare.isHidden = false
    }

 
}

The problem is that it does not work correctly. When the user clicks the button, the other element is displayed but not only in the selected row, but also in other rows as well, how can I solve this problem?

enter image description here

enter image description here

R.Valverde
  • 31
  • 2
  • 7

3 Answers3

0

The cell is being re-used and whatever is the state of that cell will still be there, in which case, try adding this to ListadoTableViewCell:

override func prepareForReuse() {
    super.prepareForReuse()
    btnShare.isHidden = true
}
Joanna J.
  • 106
  • 1
  • 3
0

You should try to save the state of the cell when you press the download button so that when the reloadData of the TableView is performed and the cellForRowAt function is called, the state of the changes in the cells is preserved, in your case the share button is shown if the download button has previously been pressed.

Here there is a project from my Github that make the functionality you need by applying MVVM Pattern https://github.com/JLPenaLopez/MyFiles I hope this helps you

This is a demostration: https://github.com/JLPenaLopez/MyFiles/blob/master/MyFilesGif.gif

App demostration

0

I solved using an answer from another post: Button action in custom UITableViewCell affects other cells

Thank you for your help.

R.Valverde
  • 31
  • 2
  • 7