簡體   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