Swift:自定义 UITableViewCell 中的 UIButton 'Unrecognised selector sent to instance'错误


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "ExerciseMenuCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ExerciseOptionTableViewCell
    let currentWorkout = workouts[indexPath.row]
    cell.nameLabel!.text = currentWorkout.name
    cell.photoImageView.image = currentWorkout.filename
    cell.startWorkout.tag = indexPath.row
    cell.startWorkout.addTarget(self, action:Selector("workoutAction:"), forControlEvents: .TouchUpInside)
    cell.infoWorkout.tag = indexPath.row
    cell.infoWorkout.addTarget(self, action:Selector("infoAction:"), forControlEvents: .TouchUpInside)
    return cell
    }

startWorkout和Infoworkout均导致应用程序崩溃,而错误消息"未识别的选择器已发送到实例"。

按钮操作中代码的示例。我正在尝试返回按钮的indexpath,以便我可以采取行动。

@IBAction func workoutAction(sender: AnyObject) {
    let buttonTag = sender.tag
    print(buttonTag)
}

确切的错误消息:

016-06-17 18:34:30.722练习[4711:245683] - [ercory.Exercisemenu启动工作:]:未识别的选择器发送给实例0x7fb47874a4b02016-06-17 18:34:30.727练习[4711:245683] ***终止App由于未被发现的例外" nsinvalidargumentException'p>

自定义单元中的一个按钮无法调用外壳视图控制器中的操作。您需要:

1)将@ibaction函数移至自定义单元类

2)从" CellFromrowatIndExpath"中删除添加目标代码,然后在自定义单元格中写入(如果执行此操作,则不需要编写@ibaction),或者创建从故事板中的按钮到@ibaction函数
3)为您的自定义单元创建代表swift

自定义uitaiteViewcell委托模式4)从您的自定义单元格给代表以获取您在视图控制器中实现的函数**不为了获得您需要cell.delegate = self,否则当代表被称为

时会崩溃

ex:

customcell.swift

protocol CustomCellDelegate {
    func pressedButton()
}
class CustomCell: UITableViewCell {
    var delegate: CustomCellDelegate!
    @IBAction func buttonPressed(sender: UIButton) {
        delegate.pressedButton()
    }
}

viewcontroller.swift

class CustomClass: UIViewController, CustomCellDelegate {
    func pressedButton() {
        // Perform segue here
    }
}

相关内容

最新更新