2017-08-16 3 views
-2

シナリオ: を私はテーブルビューとカスタムボタン電池を持っています。セルのコントローラでは、代理人がMainControllerの関数を呼び出すようにしました。 問題は私のセルのボタンアクションが発砲していますが、削除機能はありません。委任機能は、焼成・スウィフトされていない4

CustomCellコントローラー:

import UIKit 

protocol CustChatButtonDelegate: class { 
    func chatActionClick(cell: CustButtonCell) 
} 
class CustButtonCell: UITableViewCell { 

    var delegete:CustChatButtonDelegate? 
    @IBOutlet weak var btnChat: UIButton! 

    override func prepareForReuse() { 
     super.prepareForReuse() 
//  self.delegete=nil 
    } 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 
    override func setSelected(_ selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 
    @IBAction func btnChatActionClick(_ sender: Any) { 
     print("btn click") 
     self.delegete?.chatActionClick(cell: self) 
    } 

} 

メインコントローラ:

import UIKit 

class ChatController: UIViewController ,UITableViewDelegate, UITableViewDataSource, ChatButtonDelegate,CustChatButtonDelegate { 

func chatActionClick(cell: CustButtonCell) { 
     print("btnClicked") 
    } 

} 
+0

'cell.delegate = self'あなたはCustButtonCellのデリゲートプロパティを設定している –

+0

セルを作成する場所を追加しますか?また、参照サイクルを持つため、デリゲートを 'weak'として宣言することもできます。 – Palle

+0

あなたのコードの中のあなたの ' .delegate = self'(またはそれに類するもの)はどこですか?言い換えれば、あなたは 'delegate' varにどこに値を割り当てましたか? – holex

答えて

3

これはかなり一般的な問題です。あなたはセルを作成しているところ

単にcellForRowAt、例えば、cell.delegate = selfを追加します。

代理人プロパティweakも作成してください。

0

委任オブジェクトはデリゲートに弱い基準を維持しなければなりません。

weak var delegete: CustChatButtonDelegate? 

強いは、デフォルトでは、オブジェクト参照に適用されますのでご注意ください。

関連する問題