2016-10-24 11 views
0

各TableViewセルには2つのボタンがあります。 1つのボタンをタップすると、その外観と他のボタンの外観を変更したいと思います。私はapproach outlined hereを使ってタップしたボタンを変更する方法を考え出しましたが、他のボタンの調整には苦労しています。同じTableview内の異なるボタンを変更する操作上のセル(Swift)

現在、関連するコード:

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

//... 

} 
+0

あなたの 'FeedbackTableViewCell'自体で扱えないのはなぜですか? – Santosh

+0

私はそれをどうやって行うのか、あなたが何を示唆しているのか分かっていません。私が理解できる限り、別の関数を呼び出すボタンのターゲットを作成する必要があります。そのfeedbackYesButtonTapped関数では、私はセル全体にアクセスすることはできません。私はまた、feedbackYesButtonTapped関数にtableViewCell全体を渡そうとしましたが、私はそのアプローチを試みたときに何らかの理由でボタンにアクセスできませんでした。 – Ben

答えて

1

あなたが代わりのUITableViewCellのクラスの中にボタンイベントを処理した場合、あなたが簡単にそこ内の2つのボタンを参照することができると思いますので、それは、少し楽になりますが、あなたがやりたいことはまだ可能ですあなたがそれをやっている方法:

まず、ボタンを押した後にセルへの参照を取得したいと思うでしょう。セルの行をボタンのタグに設定しているようですので、そのtableViewにセクションが1つしかないと仮定します。その場合、let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: button.tag, inSection: 0))と言ってセルへの参照を得ることができます。これは、明白な理由からオプションです。したがって、安全にアンラップしてください。その後、あなたはそれをどのように扱うか分からなかった現場でturnFeedbackButtonOff(cell.feedbackNoButton)と言うことができます。

+0

フィードバックいただきありがとうございます。私はそれを試みるつもりですが、UITableViewCellのクラス内でボタンイベントを処理する方法についてもう少し詳しく話すことができますか?私はxibを使用しているので、awakeFromNib funcにイベントを追加することを意味しますか?それから、私はUITableViewをロードした他のクラスに普遍的にアクセスできるはずですか? – Ben

+0

あなたのFeebackTableViewCellクラスファイルに入り、好きなようにボタンセレクタをawakeFromNibに追加すると、こうすることになります: self.feedbackYesButton.addTarget(self、action:#selector(self.feedbackYesButtonTapped(sender :) )、のために:.touchUpInside) self.feedbackNoButton.addTarget(自己、アクション:#selector(self.feedbackNoButtonTapped(差出人:))、のために:.touchUpInside) – creeperspeak

+0

それからちょうどあなたのfeebackYesButtonTappedを移動して、あなたのFeedbackTableViewCellクラスファイル内の関数をfeedbackNoButtonTappedさて、turnFeedbackButtonOn/Off関数を呼び出すと、turnFeedbackButtonOff(self.feedbackNoButton)などと言うことができます。 – creeperspeak

関連する問題