user3591436 user3591436 - 3 months ago 5
Swift Question

UIButton inside table not triggering

Hello I'm trying to figure out how to call a UIButton inside a custom cell within a UItable in storyboard. At the moment I have a library that creates a sidemenu working just fine (more info here) and I can see the button I placed when I launch the simulator. However, when I click on the button the action is not triggered, can you please guide me as to how I can achieve this?

Important to note that the table was create entirely in storyboard.

My work in progress code within

TopratedVC.swift
to get the button to trigger the action:

override func viewDidLoad() {
super.viewDidLoad()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewVibrantCell") as! CellClassMenu

cell.sendFeedBackBtn.tag = indexPath.row
cell.sendFeedBackBtn.addTarget(self, action: "sendFeedBackBtnAction:", forControlEvents: .TouchUpInside)
cell.contentView.userInteractionEnabled = false //tried with true as well, no difference
cell.bringSubviewToFront(cell.sendFeedBackBtn)
cell.userInteractionEnabled = true

return cell
}

func sendFeedBackBtnAction(sender: UIButton){

print("sendFeedBackBtnAction tapped")
}


My
UITableViewVibrantCell.swift
file contains the following:

import UIKit

class UITableViewVibrantCell: UITableViewCell {

@IBOutlet var sendFeedBackBtn: UIButton!
}


My
sndFeedBackBtn
has a referencing outlet to
UITableViewVibrantCellsendFeedBackBtn
which has a class of
UITableViewVibrantCell
. What am I doing wrong? Thank you.

What it looks like in simulator:
enter image description here

Answer

In your post, you show a UITableViewVibrantCell class, and dequeue a cell with the "UITableViewVibrantCell" identifier, but cast it as CellClassMenu?

Anyhow, it would be better practice to create a cell delegate for actions, and let your controller decide the implementation, rather than adding a target every time the cell is dequeued. You can do that like so:

UITableViewVibrantCell

import UIKit

protocol UITableViewVibrantCellDelegate: NSObjectProtocol {
    func buttonPressed(sender: UIButton)
}

class UITableViewVibrantCell: UITableViewCell {

    var delegate: UITableViewVibrantCellDelegate?
    @IBOutlet var feedbackButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
        feedBackButton.addTarget(self, action: #selector(self.buttonPressed(_:)), forControlEvents: .TouchUpInside)
    }
    func buttonPressed(sender: UIButton) {
        delegate?.buttonPressed(sender)
    }
}

TopratedVC

class TopratedVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewVibrantCell") as! UITableViewVibrantCell
        cell.delegate = self
        return cell
    }

    // MARK: - UITableViewVibrantCellDelegate
    func buttonPressed(sender: UIButton) {
        print("feedbackButton tapped")
    }
}
Comments