UIButton没有响应在自定义UITableViewCell中使用

时间:2016-02-12 08:00:56

标签: ios swift uitableview uibutton

我知道这个问题已经在SO中被问过几次了。尽管尝试了这些,我仍然无法解决我的问题。

我在UIViewController中使用UITableView。我有一个自定义的UITableViewCell,里面有几个按钮。但是,我无法使Button响应Click事件。

开发环境是iOS 9和Swift 2

使用的片段:

BranchNearMeTableViewCell.swift包含

@IBOutlet weak var btnDetails: UIButton!

查看控制器类

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("branchNearTableCell") as! BranchNearMeTableViewCell

 cell.btnDetails.tag = indexPath.row
        cell.btnDetails.addTarget(self, action: "showDetails:", forControlEvents: .TouchUpInside)

}

func showDetails(sender: UIButton){

        print("Button Pressed:")
    }

其他信息:

TableView和TableCellView在“界面”构建器中禁用了用户交互,因为不希望整个单元格可单击。

TableViewCell中的UIButton启用了用户交互。

作为一个iOS菜鸟,我可能会犯一个我可能忽略的愚蠢错误。

我检查的类似问题包括:

SO1

SO2

SO3

我非常感谢有关此问题的任何帮助。

9 个答案:

答案 0 :(得分:5)

我也会在表格视图单元格上设置<hr /> {{name}} userInteractionEnabled。我会阻止点击使用true UITableViewallowsSelection

还要记得删除false中的目标和操作,因为单元格已被回收,按钮可能已经有了目标和操作,它可能会添加一秒。

答案 1 :(得分:4)

我遇到了类似的问题。我通过UIButton以编程方式向UITableViewCell中添加了addSubview。该按钮将不会响应触摸事件。使用Debug View Hierarchy,我最终发现添加到UITableViewCell的所有子视图都在contentView之后,这阻止了用户输入到达UIButton。通过将UIButton添加到contentView而不是UITableViewCell,解决了该问题。

答案 2 :(得分:3)

我今天遇到了这个问题,在静态 UITableview 单元格中有一个按钮,它没有响应用户事件。 我意识到单元格的“内容视图”也有一个“启用用户交互”复选框。确保在“文档大纲”菜单中的 UITableview 单元格中选择“内容视图”,然后在“属性检查器”中勾选“启用用户交互”框 - 请参阅附图以供参考。还需要检查单元格的“启用用户交互”才能使其正常工作。 希望这可以帮助。 XCcode screen shot

答案 3 :(得分:2)

对于以编程方式创建的视图,唯一需要记住的是在 lazy var 中使用 UITableViewCell 声明按钮。并且还将子视图添加到 contentView 而不是单元格本身例如:

class CounterCell: UITableViewCell {

    lazy var incrementButton: UIButton = {
        let button = UIButton()
        button.setTitle("+", for: .normal)
        button.addTarget(self, action: #selector(incrementAction), for: .touchUpInside)
        return button
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(incrementButton)
        // Your constrains here
    }

    @objc func incrementAction() {
    }

}

以编程方式使用视图时,无需添加 .userInteractionEnabled 标志。

然后要从单元格中取出操作,只需添加一个委托并从 UITableViewDataSource 中分配它。

答案 4 :(得分:2)

我找到了一个简单的解决方案:

继承UITableViewCell,并覆盖init()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    //init subviews, eg. self.switch = UISwitch()
    super.init(style: style, reuseIdentifier: reuseIdentifier)
                
    // add this line magic code 
    contentView.isUserInteractionEnabled = true
                
    //add subviews, e.g. self.addSubView(self.switch)
}

答案 5 :(得分:1)

此外,请确保您要在按钮设置以外的按钮上添加目标操作。因此,而不是

let button: UIButton = {
    //addTarget...
}()

您可以使用某个功能在发生某些情况后设置按钮:

func setButtonsUp() {
    // myButton.addTarget
}

答案 6 :(得分:0)

第一眼看到您的代码似乎没有任何问题 所以我建议你在按钮的超视图中添加背景颜色,为什么?因为如果按钮位于超级视图的框架之外,它将永远不会接收到触摸 如果您看到该按钮不在背景颜色内,则可能是您在定位项目,检查约束或您正在使用的任何内容时遇到问题。 还要检查按钮的框架 您也可以通过在运行时检查视图来完成这两项工作,here一个教程。

答案 7 :(得分:0)

我不知道代码中有什么问题,但我可以建议我个人使用它并且它适用于我

在BranchNearMeTableViewCell.swift

@IBOutlet var btnDetails: UIButton!
@IBAction func btnDetailsClick(sender: AnyObject) {
    tapButton?(self)
}
var tapButton: (UITableViewCell -> Void)?

在表视图控制器中

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("branchNearTableCell") as! BranchNearMeTableViewCell

 cell.tapButton = {(user) in
             //User will be tablecell here do whatever you want to do here
        }

}

因此,如果您点击表格单元格中的按钮,则会调用此cell.tapButton,您可以在此处执行任何操作

答案 8 :(得分:0)

您只需要做(在ViewDidLoad中):

mTableView.delaysContentTouches = false
相关问题