表视图的自定义单元格中的按钮

时间:2015-09-01 17:06:17

标签: ios iphone swift uitableview

我在tableView的自定义单元格中有一个按钮。所有按钮的功能对于不同的行是不同的。但由于我有自定义单元格,所以它不会告诉选择哪一行,当我按下按钮并运行该按钮的默认功能。任何人都可以帮我如何更改单击按钮的功能。或者如何知道选择了哪个行按钮。

3 个答案:

答案 0 :(得分:2)

您可以让按钮处理程序检查单元格的QNameUtils。这就是你知道按钮包含在哪个单元格中的方式。

答案 1 :(得分:1)

在你的cellForRowAtIndexPath中使用;

cell.yourbutton.tag = indexPath.row

cell.yourbutton.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)

然后为按钮创建一个功能

func buttonPressed(sender: UIButton) {

    var buttonCell = sender.tag

    // do what you need
    // This will do what you need for the selected button in the cell
}

答案 2 :(得分:0)

您可以通过在表格视图上调用indexPathForSelectedRow()indexPathsForSelectedRows()方法来判断选择了哪个单元格。