无法单击 UITableViewCell 内的按钮? [英] Can't click button inside UITableViewCell?

查看:27
本文介绍了无法单击 UITableViewCell 内的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 storyboard 创建一个 UITableView,加载一些数据以推荐用户关注,并在单元格内包含一个关注按钮.

唯一的问题是,我无法点击单元格内的关注按钮.

这是我的 ViewController 类中 TableView 的代码(它不是 TableViewController,它是一个包含 UITableViewDelegate 和 UITableViewDataSource 的 UIViewController):

func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) ->整数{返回 followRecommendations.count}func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->UITableViewCell {让 followCell = tableView.dequeueReusableCell(withIdentifier: "followCell", for: indexPath) as!按照推荐表ViewCellfollowCell.followButton.tag = indexPath.rowfollowCell.followButton.addTarget(self, action: #selector(self.buttonTapped), for: UIControlEvents.touchUpInside)followCell.followRecommendationName.text = followRecommendations[indexPath.row].suggestedUserNamefollowCell.followRecommendationInterests.text = followRecommendations[indexPath.row].suggestedUserTasteString让 imageUrl = followRecommendations[indexPath.row].suggestedUserImagefollowCell.followRecomendationImage.downloadedFrom(link: imageUrl)返回 followCell}func buttonTapped() {打印(点击")}

这里是 TableViewCell 类的代码.

class FollowRecommendationTableViewCell: UITableViewCell {@IBOutlet 弱变量 followRecomendationImage:UIImageView!@IBOutlet 弱变量 followRecommendationName:UILabel!@IBOutlet 弱变量 followRecommendationInterests:UILabel!@IBOutlet 弱变量 followButton:UIButton!覆盖 funcawakeFromNib() {super.awakeFromNib()}覆盖 func setSelected(_ selected: Bool, 动画: Bool) {super.setSelected(选中,动画:动画)}}

我之前在 StackOverflow 上看到过这个问题,但我没能解决这个问题.以下是我尝试过的一些方法:

  • 启用/禁用 tableView、单元格、内容视图和按钮的用户交互的多种变体.
  • 使用以下代码将按钮移动到内容视图的前面:followCell.bringSubview(toFront: followCell.followButton)
  • 我已经尝试了本教程中显示的委托/协议方法:

    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!

    解决方案

    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:

    这篇关于无法单击 UITableViewCell 内的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆