Kada
Kada

Reputation: 63

get label value on button clicked from tableview Swift

I want to get value of label and on button click without using this function

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

I want to pass that label's value to next view controller using prepareforsegue()

Upvotes: 1

Views: 819

Answers (1)

Sameh Salama
Sameh Salama

Reputation: 777

you have a cell with a UIButton and a UILabel, let's say your cell class is MyTableViewCell, your button named myButton and your label named myLabel

in cellForRowAtIndexPath set target for the button

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

then in your tableView view controller add this function

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

Upvotes: 2

Related Questions