委托函数没有触发--Swift 4

时间:2017-08-16 08:00:26

标签: ios swift uitableview custom-cell

方案: 我有一个表视图和一个自定义按钮单元格。在单元的控制器中,我已经委托调用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)
    }

}

MainController:

import UIKit

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

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

}

2 个答案:

答案 0 :(得分:4)

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

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

同时制作您的委托属性weak

答案 1 :(得分:0)

委托对象应该保持对其委托的引用。

weak var delegete: CustChatButtonDelegate?

请注意,默认情况下,会应用于对象引用。

相关问题