我在表单元格中有一个按钮,当按下它会带有错误:
未识别的选择器发送到实例0x7F9A39840A00 2016-11-25 15:32:04.310 App Name [19161:1264937] ***终止App由于未被发现的例外" NsinvalidargumentException'/p>
这是代码:
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文件
崩溃发生时
有人知道如何解决此问题吗?
似乎问题是关于forwardPress
,而不是forwardButton
和cellButtonPress
。您是否在接口构建器中检查了插座检查员?
在某些接口元素上(也许是阅读调试器时的单元格),您可能有一个未链接的插座,称为forwardPress
。您在元素上执行操作,IB寻找forwardPress
方法,该方法不存在=> crash。
对于UITableViewCell
中的UIButton
操作,您可以使用:
method -1 - 即使在CustomTableViewCell
本身中使用闭合。
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
}
方法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
}