1

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)")
}
Community
  • 1
  • 1
Octavio Rojas
  • 187
  • 13
  • I have some problem understanding your code as it is not in english. What does `eliminarProducto` mean and do? – Papershine Feb 07 '17 at 23:31
  • 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 Feb 07 '17 at 23:37
  • Check if you have connected the button to the `@IBAction` properly – Papershine Feb 07 '17 at 23:38
  • 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 Feb 07 '17 at 23:41

1 Answers1

1

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).

swillsea
  • 341
  • 4
  • 12
  • It seems you are right because now I got a this error message when clicking the button: `fatal error: unexpectedly found nil while unwrapping an Optional value` So I guess the function is being read now and I need to figure out why it is nil. – Octavio Rojas Feb 07 '17 at 23:57
  • 1
    Are you setting `indexPath` anywhere? Looking at the code you shared, it looks like you created it as an implicitly unwrapped optional, but never gave it a value. Therefore it is still nil when you call your delegate method. – swillsea Feb 08 '17 at 00:02
  • @swillsea I copied the code from this post: http://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell it seems that the indexPath isn't set because, I tried setting it to [] and now I don't get the error anymore, how and where can I set the indexPath so that I can know and store the indexPath of the row with each click? – Octavio Rojas Feb 08 '17 at 00:18
  • 1
    @OctavioRojas In your tableview `cellForRowAt` method, you can set `cell.indexPath = indexPath` – swillsea Feb 08 '17 at 00:21
  • @swillsea That was it bro, I appreciate it so much! those two instructions: `cell.delegate = self`& `cell.indexPath = indexPath`were the missing parts. You rock man! I'd like to Thank to @paper1111 for his time and assistance too, you are awesome! – Octavio Rojas Feb 08 '17 at 00:28
  • 1
    @OctavioRojas Happy to help! – swillsea Feb 08 '17 at 00:31