Swift:自定义UITableViewCell中的UIButton'无法识别的选择器发送到实例'错误

Rya*_*ton 3 uibutton uitableview ios swift

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
    }
Run Code Online (Sandbox Code Playgroud)

startWorkout和infoWorkout都会导致应用程序崩溃,并显示错误消息"无法识别的选择器已发送到实例".

按钮操作中的代码示例.我试图返回按钮的indexPath,然后我可以采取行动.

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

}
Run Code Online (Sandbox Code Playgroud)

确切的错误信息:

016-06-17 18:34:30.722练习[4711:245683] - [Exercises.ExerciseMenu beginWorkout:]:无法识别的选择器发送到实例0x7fb47874a4b0 2016-06-17 18:34:30.727练习[4711:245683]***因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [Exercises.ExerciseMenu beginWorkout:]:无法识别的选择器发送到实例0x7fb47874a4b0'

brl*_*214 7

自定义单元格内的按钮无法调用外壳视图控制器中的操作.你需要:

1)将@IBaction函数移动到自定义单元类

2)从"cellFromRowAtIndexPath"中删除添加目标代码,并将其写入自定义单元格(如果这样做,则不需要编写@IBAction)或创建连接从故事板中的按钮到@IBAction函数

3)为自定义单元格创建委托 Swift中的自定义UITableViewCell委托模式

4)从您的自定义单元格中调用您在视图控制器中实现的功能的委托**不要获取你需要cell.delegate = self,否则当调用委托时它会崩溃

例如:

CustomCell.swift

protocol CustomCellDelegate {
    func pressedButton()
}

class CustomCell: UITableViewCell {
    var delegate: CustomCellDelegate!

    @IBAction func buttonPressed(sender: UIButton) {
        delegate.pressedButton()
    }
}
Run Code Online (Sandbox Code Playgroud)

ViewController.swift

class CustomClass: UIViewController, CustomCellDelegate {

    func pressedButton() {
        // Perform segue here
    }
}
Run Code Online (Sandbox Code Playgroud)