简体   繁体   English

如何获取tableView单元格上的单击事件UIbutton?

[英]How to get click event UIbutton on tableView cell?

I have taken UIbutton on TableViewCell , but when I click on a row, only the row get clicked, but I want to click only the button on that row. 我在TableViewCell上使用了UIbutton ,但是当我单击一行时,只单击了该行,但是我只想单击该行上的按钮。

How is this possible? 这怎么可能?

In the CellForRowatindexPath define tag for button and also set target to handler the event 在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")
    }
}

First do one thing 首先做一件事

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

Then create IBOutlet of your button in Cell class ( Don't create the IBAction yet!!! ) 然后在Cell类中创建按钮的IBOutlet (请不要创建IBAction !!!

Then in your CellForRowatindexPath create the action of button like this 然后在您的CellForRowatindexPath创建按钮的操作,如下所示

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

then create the button action Function 然后创建按钮动作功能

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

Check it. 核实。 Hope it helps!!!! 希望能帮助到你!!!!

you set click event for cell Button try this way 您设置单元格按钮的点击事件,尝试这种方式

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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取UIButton在Tableview单元格中的位置,以及如何在Swift中添加目标? - How to get location of UIButton in Tableview cell, and add target in Swift? UIButton在TableView单元格中不可见 - UIButton not visible in TableView Cell iOS在TableView单元中处理事件单击按钮 - IOS handle event click button in tableView cell 如何从tableview框架中调用tableview单元格单击事件? - How can I Invoke tableview cell click event out of tableview frame? 如何在表视图单元格中的按钮单击上获取标签以进行prepareForSegue - How to get label on button click in tableview cell for prepareForSegue 如何在单击按钮时获取tableview单元格中textfield的值? - How to get the value of textfield in a tableview cell on click of a button? iOS在TableView单元格中快速显示UIButton - iOS swift UIButton in TableView Cell 单击UIButton时如何重置所有tableView数据和数组? - How to reset all tableView data and arrays when click of UIButton? 如何在tableview单元格内的collectionview单元格的按钮单击时获取索引 - How to get index on button click of collectionview cell which is inside tableview cell 如何在uitabbarcontroller中的uibutton click事件上推送otherviewcontroller? - How to Push otherviewcontroller on uibutton click event in uitabbarcontroller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM