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

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

问题描述

我知道这个问题在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

< a href =https://stackoverflow.com/questions/26736151/custom-cell-error-addtarget-button> SO3

我非常感激关于这个问题的任何帮助。

I Deeply appreciate any help regarding this question.

推荐答案

我会将 userInteractionEnabled 设置为表格视图单元格中也是。我会阻止点击使用 UITableView allowsSelection false

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:中的目标和操作,因为单元格被回收,按钮可能已经有目标和行动,它可能会增加一秒。

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.

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

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