【问题标题】:How to determine cell in tableview which have a button pressed如何确定表格视图中按下按钮的单元格
【发布时间】:2015-08-22 08:36:03
【问题描述】:

请您帮助我了解如何弄清楚我的情况。我有一个表格视图和单元格原型名称“cellWithButton”。在这个单元格上,我有一个按钮。我按住“控制”并拖动到视图控制器以对内部按钮进行触摸操作。而且我无法理解如何确定按下了按钮的单元格。

我尝试自己解决它并使用“标签”找到它。所以在 cellForRowAtIndexPath 的时候为按钮设置不同的标签。但它不起作用。 我可以在 cellForRowAtIndexPath 期间分配按钮的什么属性来确定这个按钮。

毕竟我认为我做错了什么?

【问题讨论】:

  • var butMain : UIButton = cell.viewWithTag(200) as! UIButton
  • 将 button.tag 的值设置为 IndexPath.Row 。这是最简单的方法。

标签: swift button tableview cell


【解决方案1】:

当我在函数中填充表格视图时

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->        UITableViewCell

我将任何字符串分配给单元原型上的按钮“accessibilityLabel”

var buttonOnCell : UIButton = cell.viewWithTag(200) as! UIButton
buttonOnCell.accessibilityLabel = "button number 3"

在行动中我可以访问它

@IBAction func buttonPressedOnCell(sender: UIButton) {
        println(sender.accessibilityLabel)
    }

【讨论】:

    猜你喜欢
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多