ios - 如何在 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).
解决方案
根据你的崩溃,最后一个 superView 是 contentView 那么它的 superView 是需要的单元格,所以你需要
let cell = sender.superview!.superview!.superview as! PatientUpdateVCCell
推荐阅读
- c++ - 在代码块上使用 ODEINT
- android - 并非资产文件夹中的所有项目都打包在 APK 中
- javascript - 如何使用 React 实现导航栏
- windows - 在 Docker windowsservercore 容器中运行 MFC 应用程序
- mysql - 希望 SQL Server CONSTRAINT 不允许某个特定的字符串
- unix - My exit commands are not working right
- c# - 我可以根据 .NET Core 中的运行时标识符定义常量吗?
- ipython - Jupyter 笔记本 --- (
标签)被呈现不同的颜色 - ios - QueryLimited(toLast:1) firebase Chat Mesenger Swift
- javafx - 从其他类 JavaFX 执行方法