D. Finna
D. Finna

Reputation: 467

Button on tableview cell is not working - swift

I have a button on my custom tableview cell that is showing the users name.
When you click on it, you should be taken to his/her profile but nothing happens?

Here is my custom tableview cell class (commentsTableViewCell.swift) :

import UIKit

protocol commentsTableViewCellDelegate {
    func namesIsTapped(cell: commentsTableViewCell)
}


class commentsTableViewCell: UITableViewCell {

    @IBOutlet weak var nameButton: UIButton!
    @IBOutlet weak var commentLabel: UILabel!
    @IBOutlet weak var profilePic: UIImageView!
    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var uidLabel: UILabel!

    var delegate: commentsTableViewCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func nameIsTapped(sender: AnyObject) {
        //4. call delegate method
        //check delegate is not nil
        if let _ = delegate {
            delegate?.namesIsTapped(self)
        } else {
        print("Delegate is \(delegate)")
        }
    }
}

Here is the important part of commentsTableViewController.swift:

class commentsTableViewController: UITableViewController, commentsTableViewCellDelegate, UITextViewDelegate {

    @IBOutlet weak var commentLabel: UITextView!
    @IBOutlet weak var theLabel: UILabel!

    var dataGotten = "Nothing"
    var updates = [CommentSweet]()

    func namesIsTapped(cell: commentsTableViewCell) {
        //Get the indexpath of cell where button was tapped
        //let indexPath = self.tableView.indexPathForCell(cell)

        let allDataSend = cell.nameButton.titleLabel?.text!
        self.performSegueWithIdentifier("toDetailtableViewController", sender: allDataSend)

    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "toDetailtableViewController" {
            if let nextVC = segue.destinationViewController as? theProfileTableViewController {
                nextVC.viaSegue = sender! as! String
            }
        }

    }

    override func viewDidLoad() {

Upvotes: 4

Views: 3446

Answers (2)

Ahmad F
Ahmad F

Reputation: 31645

I suggest to handle the action of the cell's button in the viewController. You can recognize which button has been tapped by setting a tag for it.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell

        cell.myButton?.tag = indexPath.row
        cell.myButton?.addTarget(self, action: #selector(), for: .touchUpInside)

        return cell
    }

    func namesIsTapped(tappedButton: UIButton) {
        // get the user (from users array for example) by using the tag, for example:
        let currentUser = users[tappedButton.tag]

        // do whatever you want with this user now...

    }

Upvotes: 4

user3608500
user3608500

Reputation: 835

Kindly set the delegate to self.

In cellforRowIndexPath

commentsTableViewCell.delegate = self

Where commentsTableViewCell is the custom UITableViewCell object

Upvotes: 6

Related Questions