[英]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.