方案: 我有一個表視圖和一個自定義按鈕單元格。在單元的控制器中,我做了一個委託來調用MainController的一個函數。 問題是我的單元格按鈕操作正在觸發,但刪除功能沒有。代表函數沒有觸發 - Swift 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)
}
}
MainController:
import UIKit
class ChatController: UIViewController ,UITableViewDelegate, UITableViewDataSource, ChatButtonDelegate,CustChatButtonDelegate {
func chatActionClick(cell: CustButtonCell) {
print("btnClicked")
}
}
在您創建單元格的地方添加'cell.delegate = self' –
您是否設置了CustButtonCell的委託屬性?此外,您可能希望將該委託聲明爲「弱」,因爲否則將有一個參考週期。 – Palle
你的代碼中的' .delegate = self'(或類似的東西)在哪裏?換句話說,你在哪裏給'delegate' var賦值了? –
holex