首页 > 解决方案 > 如何在 swift 中单击按钮时获取 tableview 标签值

问题描述

我是 swift 的新手,我想在单击按钮时从 tableview 中获取标签的值

我正在使用这样的代码,但它正在崩溃

在单元格中forrowatindexpath

 cell.btnsubmit.tag = indexPath.row
        cell.btnsubmit.addTarget(self, action: #selector(buttonSelected), for: .touchUpInside)

@objc func buttonSelected(sender: UIButton){
        print(sender.tag)

         let cell = sender.superview?.superview as! PatientUpdateVCCell

         surgery_date = cell.surgeryDateTextField.text!
         discharge_date = cell.dischargeDateTextField.text!
         follow_up_duration = cell.lblfolowup.text!
         follow_up_date = cell.firstFollowUpTextField.text!
         patient_status = cell.patientStatusTextView.text!

    }

但它正在崩溃。我怎样才能做到这一点

碰撞

Could not cast value of type 'UITableViewCellContentView' (0x11a794af0) to 'appname.PatientUpdateVCCell' (0x10ae74ae0).

标签: iosswiftuitableview

解决方案


根据你的崩溃,最后一个 superView 是 contentView 那么它的 superView 是需要的单元格,所以你需要

let cell = sender.superview!.superview!.superview as! PatientUpdateVCCell

推荐阅读