Can't click button inside UITableViewCell?
Asked Answered
F

1

1

I'm using storyboard to create a UITableView, loading up some data to recommend users to follow, and including a follow button inside the cell.

Only problem is, I'm not able to click the follow button inside the cell.

Here's the code for the TableView in my ViewController class (It isn't a TableViewController, it's a UIViewController that includes UITableViewDelegate and UITableViewDataSource):

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return followRecommendations.count
}

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

    followCell.followButton.tag = indexPath.row
    followCell.followButton.addTarget(self, action: #selector(self.buttonTapped), for: UIControlEvents.touchUpInside)

    followCell.followRecommendationName.text = followRecommendations[indexPath.row].suggestedUserName
    followCell.followRecommendationInterests.text = followRecommendations[indexPath.row].suggestedUserTasteString

    let imageUrl = followRecommendations[indexPath.row].suggestedUserImage
    followCell.followRecomendationImage.downloadedFrom(link: imageUrl)

    return followCell
}

func buttonTapped() {
    print("Tapped")
}

and here's the code for the TableViewCell class.

class FollowRecommendationTableViewCell: UITableViewCell {

@IBOutlet weak var followRecomendationImage: UIImageView!
@IBOutlet weak var followRecommendationName: UILabel!
@IBOutlet weak var followRecommendationInterests: UILabel!
@IBOutlet weak var followButton: UIButton!

override func awakeFromNib() {
    super.awakeFromNib()
}

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

I've seen this question on StackOverflow before, but I wasn't able to fix the problem. Here are some things I've tried:

  • Numerous variations of enabling/disabling user interaction for the tableView, cell, content view, and button.
  • Moving the button to the front of the content view using this code: followCell.bringSubview(toFront: followCell.followButton)
  • I've tried the delegate/protocol method shown in this tutorial: http://candycode.io/how-to-properly-do-buttons-in-table-view-cells/

I just can't seem to get it to work.

Has anyone else had this issue and figured out a solution?

Thanks in advance!

Floranceflore answered 5/4, 2017 at 7:17 Comment(0)
S
1

I checked - your code works fine. You probably don't have IBOutlet set for your button on the cell, either in Storyboard, (or in separate .xib file if you use it). Open your storyboard and match the outlet from the cell to button, like this:

Set the outlet from your cell to the UIButton in Interface Builder

Sidman answered 5/4, 2017 at 7:53 Comment(3)
I checked storyboard and I do have the outlet set for the button on the cell. Still doesn't work.Floranceflore
@Floranceflore then I suggest you check things as proper classes set for cell, controller, cell identifier and so on. Maybe outlets for dataSource or table view delegate are not set? There is probably something wrong set in interface builder or naming. Working code is in this repo: github.com/wladyslaws/tableViewFixSidman
I fixed it by enabling multiple touch on the content view, which was the difference between your storyboard and mine. Thanks for your help!Floranceflore

© 2022 - 2024 — McMap. All rights reserved.