如何在tableView单元格上获取单击事件UI按钮?

时间:2015-06-25 11:01:39

标签: ios swift uitableview cocoa-touch uibutton

我已在UIbutton上使用了TableViewCell,但是当我点击一行时,只会点击该行,但我只想点击该行上的按钮。

这怎么可能?

3 个答案:

答案 0 :(得分:2)

在按钮的CellForRowatindexPath定义标记中,还设置目标以处理事件

button.tag=indexPath.row;
button.addTarget(self, action: "buttonHandler:", forControlEvents: UIControlEvents.TouchUpInside)

*************
func buttonHandler(sender:UIButton!)
{
    if(sender.tag==0){
         println("Button at row 0")
    }
    else if(sender.tag==1){
       println("Button at row 1")
    }
}

答案 1 :(得分:0)

首先做一件事

cell.selectionStyle = UITableViewCellSelectionStyleNone;
 // This will disable the selection highlighting.

然后在Cell类中创建按钮的IBOutlet不要创建IBAction !!!

然后在你的CellForRowatindexPath中创建按钮的动作

  button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

然后创建按钮动作函数

func buttonAction(sender:UIButton!)
{
    println("Button tapped")
}

检查一下。希望它有所帮助!!!!

答案 2 :(得分:0)

为单元格设置单击事件按钮尝试这种方式

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {   
 cell.yourButton.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
}


func buttonClicked(sender:UIButton!)
{
    println("Button tapped")
}
相关问题