Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios tableview单元格中的Swift按钮回调不工作_Ios_Swift_Uitableview_Closures - Fatal编程技术网

Ios tableview单元格中的Swift按钮回调不工作

Ios tableview单元格中的Swift按钮回调不工作,ios,swift,uitableview,closures,Ios,Swift,Uitableview,Closures,我想在我的TableViewCell中有一个ui按钮,我尝试了一个闭包,但它没有做任何事情: 单元格: let favouriteButton: UIButton = { let v = UIButton() v.setImage(UIImage(systemName: "star"), for: .normal) v.setImageTintColor(.darkCustom) v.addTarget(self, action: #selec

我想在我的
TableViewCell
中有一个
ui按钮
,我尝试了一个
闭包
,但它没有做任何事情:

单元格

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
}
我错过了什么

typealias FavouriteButtonTappedCallback = () -> Void   
var favouriteButtonTappedCallBack = FavouriteButtonTappedCallBack?


 if let tappedClosure = self.favouriteButtonTappedCallback {
            tappedClosure()
        }


检查所有视图层次结构并为所有视图启用userinteraction,这可能是主要问题。

我发现了问题。。我调用了
self.addSubView(FavoriteButton
,因此
contentView
实际上覆盖了该按钮。只需调用以下命令即可修复:

contentView.addSubview(favouriteButton)

谢谢你的帮助!

这应该可以正常工作。@Manav我就是这么想的..检查你的委托设置,其他的看起来很好你的意思是什么委托?我设置了tableView.delegate和dataSourceyes@Chris因为我知道你的VC不是TableViewVCI的子类我有这样的:
扩展社区视图控制器:UITableViewDelegate,UITableViewDataSource
无论您在哪里使用tableview数据源和代理,您都应该引用您的代理vc.delegate=self例如,我可以选择一行,所以我认为这不是问题:/n您是否已将按钮的交互功能设置为true?这对我很有帮助!谢谢!
contentView.addSubview(favouriteButton)