从tableview Swift点击按钮获取标签值

时间:2016-08-06 14:19:22

标签: ios swift2 tableview

我希望获得标签和按钮点击的价值而不使用此功能

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
...
}

我想使用prepareforsegue()

将该标签的值传递给下一个视图控制器

1 个答案:

答案 0 :(得分:2)

你有一个带有UIButton和UILabel的单元格,让我们说你的单元格类是MyTableViewCell,你的按钮名为myButton,你的标签名为myLabel

在cellForRowAtIndexPath中设置按钮的目标

 cell.myButton.addTarget(self, action: #selector(self.cellButtonAction(_:)), forControlEvents: UIControlEvents.TouchUpInside)

然后在你的tableView视图控制器中添加这个函数

func cellButtonAction(sender: UIButton) {

    //button (sender) superview is the contentView of the cell (not the cell itself)
    let cell = sender.superview?.superview as! MyTableViewCell
    print(cell.myLabel.text!)
}