【问题标题】:How to get click event UIbutton on tableView cell?如何在 tableView 单元格上获取单击事件 UIbutton?
【发布时间】:2015-09-11 22:49:38
【问题描述】:

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

这怎么可能?

【问题讨论】:

  • 我不清楚您单击时要执行的操作。您是说要在 TableView 单元格中单击 UIButton 来执行某些操作吗?
  • 是的,没错,我已经在 TableView 单元格上使用了 Button
  • 将单元格的选择属性设置为无。
  • 是的,已经这样做了,但是之后,它不允许单击该单元格的按钮

标签: ios swift uitableview cocoa-touch uibutton


【解决方案1】:

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

【讨论】:

  • 告诉我,对你有帮助吗?
【解决方案2】:

先做一件事

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

检查一下。希望对你有帮助!!!!

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2014-07-20
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多