0
votes

I have a custom UIView with 3 tableviews, the tableviews are self-sizing depending on the content they have, and they a show a different nib when they are empty than when they have content. All of this is embedded in a scroll view and it's working already, my view scrolls and the tableviews display the content appropriately and they autosize properly, the scroll view's size is adjusting without problems either.

In two of the tableviews when the tableviews have items there are some buttons in the cell nib that I want to access along with the index of the cell clicked inside the View controller where I have defined the Table view.

enter image description here

Here in the pic I'm showing the tableViews each with one item, the Canastas tableView has just a delete item button and the Productos tableView has a delete item button and a stepper to increase or decrease the amount.

I found a solution involving delegates in StackOverflow Get button click inside UI table view cell, which I implemented. But for some reason it isn't working and the code in the delegate method isn't being executed.

Here is my code:

CanastasViewCell

import UIKit

protocol CanastasViewCellDelegate: class {
    func closeButtonTapped(at index: IndexPath)
}

class CanastasViewCell: UITableViewCell {

@IBOutlet weak var imagenProducto: UIImageView!

@IBOutlet weak var nombreProducto: UILabel!
@IBOutlet weak var descProducto: UILabel!
@IBOutlet weak var precioProducto: UILabel!

@IBOutlet weak var closeButton: UIButton!

weak var delegate: CanastasViewCellDelegate?

var indexPath: IndexPath!

override func awakeFromNib() {
    super.awakeFromNib()

}

@IBAction func eliminarProducto(_ sender: AnyObject) {
        self.delegate?.closeButtonTapped(at: indexPath)
    }

}

CarritoViewController

import UIKit

class CarritoViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource, CanastasViewCellDelegate {

 func closeButtonTapped(at index: IndexPath) {
    print("Button tapped at index:\(index)")
}
1
I have some problem understanding your code as it is not in english. What does eliminarProducto mean and do? - paper1111
eliminarProducto means deleteProduct and it should delete the product from the indexPath.row (I still haven't coded that part yet) so right now it should just print Button tapped at index:(indexPath.row) but it doesn't print out anything. - Octavio Rojas
Check if you have connected the button to the @IBAction properly - paper1111
Thanks but I already checked it and yes it is connected properly, if I delete the self.delegate?.closeButtonTapped(at: indexPath) and I write print("Test succeeded") it does print that when I click it, but when I give the instruction to execute the delegate function it does nothing. - Octavio Rojas

1 Answers

1
votes

It looks like you're not setting the delegate inside CarritoViewController. You'll want to make sure that you set cell.delegate = self inside func tableView(UITableView, cellForRowAt: IndexPath).