将按钮放在tableview单元格,无法识别的选择器时崩溃

时间:2016-05-03 09:34:27

标签: swift uibutton tableviewcell

  

PatientTableViewCell viewBtn:]:发送到实例的无法识别的选择器   0x7fdde280ac00

     

libc ++ abi.dylib:以未捕获的类型异常终止   NSException

有什么想法吗?感谢

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let current_patient = patients[indexPath.row]
    let cell = myTable.dequeueReusableCellWithIdentifier("patientCell", forIndexPath: indexPath) as! PatientTableViewCell
    let type = current_patient.enrollable_type
    var unique_id = current_patient.unique_id
    cell.patientTypelbl.text = type
    cell.patientIDlbl.text = unique_id
    cell.viewBtn.tag = indexPath.row
    cell.viewBtn.addTarget(self, action: "viewDetailAction:", forControlEvents: .TouchUpInside)
    return cell
}

   @IBAction func viewDetailAction(sender: UIButton){
    print("clicked")
    self.performSegueWithIdentifier("patientDetailCell", sender: self)
}

for patientTableviewcell:

 class PatientTableViewCell: UITableViewCell {

@IBOutlet weak var viewBtn: UIButton!
@IBOutlet weak var titleType: UILabel!
@IBOutlet weak var titleID: UILabel!
@IBOutlet weak var patientTypelbl: UILabel!
@IBOutlet weak var patientIDlbl: UILabel!
override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}

3 个答案:

答案 0 :(得分:1)

您的错误是从xCode编辑器设置IBAction,并以编程方式将选择器目标添加到同一按钮..

首先从引用插座中移除您的IBAction设置,如下图所示:

enter image description here

之后,您必须通过删除IBAction标记来重命名您的功能:

func viewDetailAction() {...}

答案 1 :(得分:0)

实际上@kennytm说的是真的,但它只是当下的警告,但最后你需要使用语法:action : #selector(YourViewController.yourFunctionName)。 您的问题是,您向viewBtn添加了viewDetailAction的目标,但这必须是一个功能,而不是故事板中的操作。

您可以声明此功能:

func viewDetailAction() {
    print("clicked")
    self.performSegueWithIdentifier("patientDetailCell", sender: self)
}

并删除您的操作:

 @IBAction func viewDetailAction(sender: UIButton){
    print("clicked")
    self.performSegueWithIdentifier("patientDetailCell", sender: self)
}

答案 2 :(得分:0)

你有两个选择

  1. 在单元格中注释行索引路径中的以下行,并检查按钮操作是否存在。 cell.viewBtn.addTarget(self,action:“viewDetailAction:”,forControlEvents:.TouchUpInside)

  2. 使用您提到的代码并重命名按钮操作方法名称 “@IBAction”