首页 > 解决方案 > UITableViewCell 问题中的 UIButton:无法识别选择器

问题描述

所以我一直在搜索 StackExchange 如何将 UIButton 放在 UITableViewCell 中,并认为我找到了答案,但一直"unrecognized selector sent to instance"出错。

这是我调用函数的地方

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tblTeams.dequeueReusableCell(withIdentifier: "cellReuse")! as! TeamsTableViewCell

    cell.btnLeave.tag = indexPath.row
    cell.btnLeave.addTarget(self, action: "LeaveTeam:", for: UIControlEvents.touchUpInside)

    return cell
}

这就是功能所在。它与前面的代码块在同一类甚至扩展中

@IBAction func LeaveTeam(sender: UIButton){
}

我试过改写报价,我试过使用#selector......请告诉我我是如何做对的。谢谢!

标签: iosswiftxcodeuitableview

解决方案


代替

cell.btnLeave.addTarget(self, action: #selector(leaveTeam(_:)) for: UIControlEvents.touchUpInside)

@objc func leaveTeam(_ sender: UIButton) {---}

小写开始方法名


推荐阅读