0

I have a UITableView list with a button which says "Click Me!". I tried following this answer below: https://stackoverflow.com/a/53043358/7746248 to tie the button to an action, but that didn't work for unknown reasons.

I have checked other ways to tie a button to an event, I have had no luck.

import UIKit

class SampleTableViewCell: UITableViewCell {
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var button: UIButton!

    var tapCallback: (() -> Void)?
    @IBAction func didTap(_ sender: Any) {
        tapCallback?()
    }
}

class TableViewController: UITableViewController {

    var tableArray = ["New York", "Chicago", "North Island"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        self.clearsSelectionOnViewWillAppear = false

        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.reloadData()
    }

    // MARK: - Table view data source

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.tableArray.count
    }

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

        // Configure the cell...
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        let names = self.tableArray[indexPath.row]
        cell.name.text = names

        cell.tapCallback = {
            // do stuff
            DispatchQueue.main.async {
                let alert = UIAlertController(title: "title", message: "Button Clicked!", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
                self.present(alert, animated: true)
            }
        }

        return cell
    }

}

enter image description here

Any other simple way to do this?

11
  • You just want to button method inside TableView right? Commented Jul 16, 2019 at 11:39
  • @KetanOdedra Yes, right now It's not working. I am trying to create an Alert message when button is clicked inside TableView. Commented Jul 16, 2019 at 11:40
  • I just implemented this and it is working properly Commented Jul 16, 2019 at 11:42
  • 1
    @truthsayer I have the answer for you, here you go del.dog/hexihodapa.m Commented Jul 16, 2019 at 12:06
  • 1
    @truthsayer yups I missed to call delegate, now check again . you have to call cell delegate in cellforrow method del.dog/hexihodapa Commented Jul 16, 2019 at 13:07

1 Answer 1

0

Add target inside cellForRowAt like below

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

        // Configure the cell...
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        let names = self.tableArray[indexPath.row]
        cell.name.text = names

        cell. button.addTarget(self, action: #selector(alertMethod), for: .touchUpInside)

        return cell
    }

@objc fileprivate func alertMethod() {
                let alert = UIAlertController(title: "title", message: "Button Clicked!", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: nil))
                self.present(alert, animated: true)
            }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.