【问题标题】:UIButton in table cell "unrecognized selector sent to instance"表格单元格中的 UIButton“发送到实例的无法识别的选择器”
【发布时间】:2016-11-26 00:32:00
【问题描述】:

我在表格单元格中有一个按钮,当按下它时,应用程序会因错误而崩溃:

无法识别的选择器发送到实例 0x7f9a39840a00 2016-11-25 15:32:04.310 应用程序名称 [19161:1264937] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[App_Name.routineCell forwardPress:]:无法识别的选择器发送到实例 0x7f9a39840a00”

代码如下:

   internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return routineGroups.count
}

func cellButtonPress() {
    print("works")
}
internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell:routineCell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! routineCell

    cell.textLabel?.text = routineGroups[indexPath.row]

    cell.forwardButton.tag = indexPath.row
    cell.forwardButton.addTarget(self, action: #selector(routinesGroups.cellButtonPress), for: UIControlEvents.touchUpInside)

    return cell
}

我在这里查看了解决方案:Link1 和这里 Link2 但我每次都遇到相同的错误。 单元格有它自己的 .swift 文件,它被拖动为一个出口: Cell.swift file

当崩溃发生时,Xcode 将我带到 AppDelegate.swift 并显示:crash goto

有谁知道如何解决这个问题?

【问题讨论】:

  • 你在你的应用中使用forwardPress吗?
  • selector 方法必须在target 指定的类中实现。 self 是当前类。要么实现routineCell 中的方法,然后更改目标或更改选择器。除此之外,以小写字母开头的命名类非常混乱。
  • @ArtemNovichkov 有一点 - 错误不是来自给定的代码。在其他地方,你一定是打电话给forwardPress:
  • @ganzogo 我没有调用 forwardPress: 代码中的任何地方 - 唯一出现的地方是错误
  • 是否有可能从您的依赖项之一调用它?如果没有,那我就难住了。

标签: ios swift swift3


【解决方案1】:

似乎问题出在forwardPress,而不是forwardButtoncellButtonPress。你检查过 Interface Builder 中的 Outlet Inspector 吗?

在某些界面元素(可能是读取调试器时的单元格)上,您可能有一个未在名为forwardPress 的代码中链接的插座。您对元素执行操作,IB 会查找不存在的 forwardPress 方法 => 崩溃。

【讨论】:

  • 这个成功了!!我检查了 ViewController 的插座检查器,但没有检查单元格本身。
【解决方案2】:

对于UITableViewCell 中的UIButton 操作,您可以使用:

Method-1 - 即使在 CustomTableViewCell 本身中也使用 closure 处理按钮点击。

CustomTableViewCell类:

class CustomTableViewCell: UITableViewCell
{
    @IBOutlet weak var forwardButton: UIButton!
    var completionHandler : (()->())?

    @IBAction func cellButtonPressed(_ sender: UIButton)
    {
        if let handler = self.completionHandler
        {
            handler()
        }
    }
}

UITableViewDataSource方法:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        cell.completionHandler = {[weak self] in self?.cellButtonPress()}
        return cell
    }

Method-2 - 在控制器级别处理按钮点击事件。

UITableViewDataSource方法:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell", for: indexPath) as! CustomTableViewCell
        cell.forwardButton.addTarget(self, action: #selector(cellButtonPress), for: .touchUpInside)
        return cell
    }

【讨论】:

    猜你喜欢
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多