Samah Ahmed
Samah Ahmed

Reputation: 419

Display selected row data from UITableView in UIButton

I wrote this code to display the selected item from UITableView on UIButton. The problem is that UIButton title is not changing on selecting row but it changes after clicking on another item and put the label of button for the first selection.

How can I directly display the data on a click ot a UITableView row.

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    countryButton.setTitle(names[indexPath.row], forState: UIControlState.Normal)
}

Note : names is my array

Upvotes: 0

Views: 91

Answers (2)

Chirag D jinjuwadiya
Chirag D jinjuwadiya

Reputation: 643

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    countryButton.setTitle(names[indexPath.row], forState: UIControlState.Normal)
}

Upvotes: 1

TangZijian
TangZijian

Reputation: 354

Because you implement it in tableView: didDeselectRowAtIndexPath:, you should implement it in tableView: didSelectRowAtIndexPath: instead. Be careful with the names. :)

Upvotes: 2

Related Questions