-1

I'm not sure if this question has already been asked but I couldn't find one asked for Swift after spending some time. A similar question could have been asked here, but it's for Objective-C and the question was vice-versa what I'm after.

I have a UIButton inside a TableViewCell which has some action once tapped on it, however, when UIButton is clicked, only didSelectRowAt tableView function is getting triggered. The UIButton is in a separate TableViewCell class. The TableViewCell is expandable, so when each row is tapped it expands/collapses. I'm sure there must be a way of controlling this with UITapGestureRecognizer, but I wouldn't know how to manipulate coordinates as I'm relatively new to Swift.

SomeTableViewCell.swift

class SomeTableViewCell: UITableViewCell {

    @IBAction func activateButtonTapped(_ sender: Any) {
        activateTapButton?(self)
    }

    var activateTapButton: ((UITableViewCell) -> Void)?
}

ViewController.swift

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet var someTableView: UITableView!

    var selectedIndex: Int = -1
    var someNumber = 123456789

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = servicesTableView.dequeueReusableCell(withIdentifier: “cell", for: indexPath) as! SomeTableViewCell
        cell.activateButton.isUserInteractionEnabled = true

        cell.activateTapButton = {(Void) in

            if let url = URL(string: "tel://\(someNumber)"), UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(selectedIndex == indexPath.row) {
            return 280
        } else {
            return 60
        }
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //indexPath handling once cell clicked

        // Expanding cell feature
        if (selectedIndex == indexPath.row) {
            selectedIndex = -1
        } else {
            selectedIndex = indexPath.row
        }
        self. someTableView.beginUpdates()

        self. someTableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        self. someTableView.endUpdates()

}
Curtis
  • 1,157
  • 4
  • 17
  • 30
  • I have actually used your code to build the button inside a `TableViewCell`, but unfortunately, button is getting ignored as explained above, I think I need to somehow give priority to UIButton touch area, so whenever button is tapped don't trigger didSelectRowAt etc – Curtis Jan 21 '18 at 23:05
  • Huh, interesting. I have the exact same situation you explained, with button and tableviewcell linking to different actions, and it works fine. Can you post your code for the button action, and `didSelectRowAt`? – Xcoder Jan 21 '18 at 23:19
  • Ok I have added the code – Curtis Jan 21 '18 at 23:46
  • Do you want the cell to expand when the button is clicked? – Xcoder Jan 22 '18 at 00:06
  • It already expands when clicked, but button inside the cell can't be tapped because didSelectRowAt takes precedence – Curtis Jan 22 '18 at 00:08
  • Make sure each of the button's parents has `isUserInteractionEnabled = true`, as it propagates to children. Touch events will generally be sent to the frontmost control that has user interaction enabled, which in your case may be the table view cell. – Samah Jan 22 '18 at 00:40
  • isUserInteractionEnabled is true by default, but I added this anyway and it's still not working – Curtis Jan 23 '18 at 08:15
  • Did people downvote the question without proper understanding it? I'm losing my faith in StackOverflow – Curtis Jan 23 '18 at 08:17

1 Answers1

0

Thought might be worth posting up the answer.

It was simply because the label was getting in the way of UIButton, just changing the layer hierarchy, the button can be tapped now.

Curtis
  • 1,157
  • 4
  • 17
  • 30