[英]Swift button callback in tableview cell not working
我想在我的TableViewCell
里面有一個UIButton
並且我用一個closure
嘗試了它,但它沒有做任何事情:
Cell
:
let favouriteButton: UIButton = {
let v = UIButton()
v.setImage(UIImage(systemName: "star"), for: .normal)
v.setImageTintColor(.darkCustom)
v.addTarget(self, action: #selector(favouriteButtonTapped), for: .touchUpInside)
v.imageView?.contentMode = .scaleAspectFit
v.contentHorizontalAlignment = .fill
v.contentVerticalAlignment = .fill
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
var favouriteButtonTappedCallback: (() -> ())?
@objc func favouriteButtonTapped() {
self.favouriteButtonTappedCallback?()
}
CellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: SearchBarCell.reuseID) as! SearchBarCell
cell.favouriteButtonTappedCallback = {
print("tapped")
}
return cell
}
我在這里缺少什么?
我發現了這個問題..我打電話給self.addSubView(favouriteButton
所以contentView
實際上覆蓋了按鈕。只需通過調用它來修復它:
contentView.addSubview(favouriteButton)
感謝所有的幫助!
typealias FavouriteButtonTappedCallback = () -> Void
var favouriteButtonTappedCallBack = FavouriteButtonTappedCallBack?
if let tappedClosure = self.favouriteButtonTappedCallback {
tappedClosure()
}
和
cell.favouriteButtonTappedCallBack
檢查所有視圖層次結構並為所有用戶啟用用戶交互,這可能是主要的嫌疑人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.