Ben Ben - 1 month ago 12
iOS Question

Change Different Button in same Tableview Cell on Action (Swift)

I have two buttons within each TableView cell. When one button is tapped I want to change its appearance AND the appearance of the other button. I figured out how to change the tapped button using the approach outlined here, but am struggling with adjusting the other button.

Current relevant code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell:FeedbackTableViewCell = self.feedbackTableView.dequeueReusableCell(withIdentifier: "cell") as! FeedbackTableViewCell

// Setup YES / NO Buttons
cell.feedbackYesButton.addTarget(self, action: #selector(MainSearchViewController.feedbackYesButtonTapped(sender:)), for: .touchUpInside)
cell.feedbackNoButton.addTarget(self, action: #selector(MainSearchViewController.feedbackNoButtonTapped(sender:)), for: .touchUpInside)

cell.feedbackYesButton.tag = indexPath.row
cell.feedbackNoButton.tag = indexPath.row

return cell
}


func feedbackYesButtonTapped (sender:UIButton) {

let yesButtonTag = sender.tag

switch yesButtonTag {
case 0:

// If YES button was not selected or was NO, then save value as YES and turn button "on", plus turn NO button "off".
turnFeedbackButtonOn(sender)
turnFeedbackButtonOff(NOT SURE HOW TO HANDLE THIS?)
}
// Other cases handled accordingly.
default:
return
}
}

//MARK: - Functions to change the appearances of feedback buttons
func turnFeedbackButtonOn(_ button: UIButton) {

button.setTitleColor(UIColor(red: 157/255, green: 249/255, blue: 88/255, alpha: 1 ), for: UIControlState())
button.titleLabel?.font = UIFont(name: "Avenir-Black", size: 18)
}

func turnFeedbackButtonOff(_ button: UIButton) {

button.setTitleColor(UIColor.black, for: UIControlState())
button.titleLabel?.font = UIFont(name: "Avenir", size: 17)
}


I tried passing the other button through with the target buttons, but I get an error when trying this. It feels like this should work, but I'm no expert at Swift so would appreciate any help!

cell.feedbackYesButton.addTarget(self, action: #selector(MainSearchViewController.feedbackYesButtonTapped(cell.feedbackYesButton, otherButton:cell.feedbackNoButton)), for: .touchUpInside)

func feedbackYesButtonTapped (sender:UIButton, otherButton:UIButton) {

//...

}

Answer

It would be a little easier if you handled the button events within the UITableViewCell's class instead, since you'd be able to easily reference the two buttons within there, but it's still possible to do what you want the way you're doing it:

First you'll want to get a reference to the cell after the button is pushed. It looks like you're setting the cell's row to be the button's tag, so I assume you've only got 1 section in that tableView. In that case you can get a reference to the cell by saying let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: button.tag, inSection: 0)). This return an optional, for obvious reasons, so you'll want to make sure to unwrap it safely. Then you can say turnFeedbackButtonOff(cell.feedbackNoButton) in the spot you were not sure how to handle it.

Comments