繁体   English   中英

更改同一Tableview单元格中的不同按钮(快速操作)

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

每个TableView单元格中都有两个按钮。 轻按一个按钮后,我想更改其外观以及另一按钮的外观。 我想出了如何使用此处概述方法来更改点击的按钮,但是在调整其他按钮方面却很费力。

当前相关代码:

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

我尝试将其他按钮与目标按钮一起传递,但是尝试此操作时出现错误。 感觉像这样应该可以,但是我不是Swift的专家,因此不胜感激!

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

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

//...

}

如果您改为在UITableViewCell的类中处理按钮事件,则会容易一些,因为您可以轻松地引用其中的两个按钮,但是仍然可以按照自己的方式进行操作:

首先,您需要在按下按钮后获得对该单元格的引用。 看起来您要将单元格的行设置为按钮的标签,所以我假设您在tableView中只有1个部分。 在这种情况下,您可以说出let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: button.tag, inSection: 0))来获得对该单元格的引用。 由于明显的原因,此方法返回一个可选的选项,因此您需要确保安全地将其打开。 然后您可以在不确定如何处理的地方说turnFeedbackButtonOff(cell.feedbackNoButton)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM