代表功能未触发-Swift 4

Sha*_*dge -2 uitableview custom-cell ios swift

场景:我有一个表格视图和一个自定义按钮单元。在单元格的控制器中,我已委托一个函数调用MainController的函数。问题是我的单元格按钮动作正在触发,但删除功能却未触发。

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

}
Run Code Online (Sandbox Code Playgroud)

主控制器:

import UIKit

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

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

}
Run Code Online (Sandbox Code Playgroud)

Pra*_*tti 5

这是一个相当普遍的问题。

只需添加cell.delegate = self创建单元的位置,例如中cellForRowAt

同时使您的委托财产weak