自定义 UITableViewCell 中使用的 UIButton 没有响应 [英] UIButton not responding used in a custom UITableViewCell

查看:27
本文介绍了自定义 UITableViewCell 中使用的 UIButton 没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know this issue is already been asked few times in SO. Despite trying those out, I am still unable to solve my problem.

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

I am using a UITableView inside a UIViewController. I have a custom UITableViewCell which has couple of buttons in it. However, I am not able to make the Button respond to Click event.

开发环境为 iOS 9 和 Swift 2

The development environment is iOS 9 and Swift 2

使用的片段:

BranchNearMeTableViewCell.swift 包含

BranchNearMeTableViewCell.swift contains

@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 在界面生成器中禁用了用户交互,因为不希望整个单元格都可以点击.

TableView and TableCellView has User interaction disabled in Interface builder since don't want the entire cell to be clickable.

TableViewCell 内的 UIButton 启用了用户交互.

UIButton inside TableViewCell has User Interaction enabled.

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

Being an iOS noob, I may be making a silly mistake which I might have overlooked.

我检查的类似问题包括:

Similar questions that I checked include:

SO1

SO2

SO3

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

I Deeply appreciate any help regarding this question.

推荐答案

我也会在表格视图单元格上将 userInteractionEnabled 设置为 true.我会阻止使用 UITableView allowsSelectionfalse

I would have userInteractionEnabled set to true on the table view cell as well. I would prevent taps using the UITableView allowsSelection to false

还要记得去掉tableView:cellForRowAtIndexPath:中的target和action,因为cell被回收了,按钮可能已经有了target和action,可能会加一秒.

Also remember to remove the target and action in tableView:cellForRowAtIndexPath: since the cells are recycled, the button might already have the target and action, it might add a second.

这篇关于自定义 UITableViewCell 中使用的 UIButton 没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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