[英]UIButton in table cell “unrecognized selector sent to instance”
I have a button in a table cell, when pressed it crashes the app with the error: 我在表格单元格中有一个按钮,当按下它会使应用程序崩溃并显示错误:
unrecognized selector sent to instance 0x7f9a39840a00 2016-11-25 15:32:04.310 App Name[19161:1264937] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[App_Name.routineCell forwardPress:]: unrecognized selector sent to instance 0x7f9a39840a00'
无法识别的选择器发送到实例0x7f9a39840a00 2016-11-25 15:32:04.310应用程序名称[19161:1264937] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[App_Name.routineCell forwardPress:]:无法识别的选择器发送到实例0x7f9a39840a00'
Here's the code: 这是代码:
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
}
I looked at the solutions here: Link1 and here Link2 but I get the same error every time. 我在这里查看了解决方案: Link1和Link2,但是每次都遇到相同的错误。 The cell has its own .swift file where it was dragged as an outlet: Cell.swift file
该单元具有自己的.swift文件,将其作为出口拖到其中: Cell.swift文件
When the crash happens Xcode takes me to the AppDelegate.swift and shows this: crash goto 当崩溃发生时,Xcode将我带到AppDelegate.swift并显示以下内容: crash goto
Does anyone know how to fix this? 有谁知道如何解决这一问题?
Seems like the problem is about forwardPress
, not forwardButton
nor cellButtonPress
. 似乎问题出在
forwardPress
,而不是forwardButton
或cellButtonPress
。 Did you check the Outlet Inspector in the Interface Builder? 您是否在Interface Builder中检查了插座检查器?
On some interface element (maybe the cell when reading the debugger), you may have an outlet not linked in code called forwardPress
. 在某些接口元素上(可能是读取调试器时的单元格),您可能有一个未链接到名为
forwardPress
代码的forwardPress
。 You perform the action on the element, IB looks for the forwardPress
method, which does not exist => crash. 您在元素上执行操作,IB将查找
forwardPress
方法,该方法不存在=>崩溃。
For UIButton
action in a UITableViewCell
, you can use: 对于
UITableViewCell
UIButton
操作,可以使用:
Method-1 - Handle the button tap even in the CustomTableViewCell
itself using closure . 方法1-使用闭包即使在
CustomTableViewCell
本身中也处理按钮点击。
CustomTableViewCell
class: CustomTableViewCell
类:
class CustomTableViewCell: UITableViewCell
{
@IBOutlet weak var forwardButton: UIButton!
var completionHandler : (()->())?
@IBAction func cellButtonPressed(_ sender: UIButton)
{
if let handler = self.completionHandler
{
handler()
}
}
}
UITableViewDataSource
method: 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 - Handle the button tap event at the controller level. 方法2-在控制器级别处理按钮轻击事件。
UITableViewDataSource
method: 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.