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) {
//...
}