gpt4 book ai didi

ios - 如何在 swift 中从选定的 tableviewcell 中过滤数组?

转载 作者:行者123 更新时间:2023-11-29 05:41:17 24 4
gpt4 key购买 nike

我对如何构建关于如何从 tableviewcell 中过滤所选数组的代码感到非常困惑。下面的 JSON 是 tableview 的内容,显示如 this

[
{
"hospitalNumber": "00000001",
"patientName": "Test Patient",
"totalAmount": 1111.3
},
{
"hospitalNumber": "00000002",
"patientName": "Test Patient 2",
"totalAmount": 1312
},
{
"hospitalNumber": "00000003",
"patientName": "Test Patient 3",
"totalAmount": 475
}
]

问题是如何在下一个View Controller中显示所选的hospitalNumberpatentName,它将显示为this

这就是我的“PaymentDetailsViewController”:

 var patientList: [Patient]! {
didSet {
latestCreditedAmountTableView.reloadData()
}
}

override func viewDidLoad() {
super.viewDidLoad()

getPatientList()

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showPatientPaymentDetailsVC" {
if let patientPaymentDetailsVC = segue.destination as? PatientPaymentDetailsViewController {
patientPaymentDetailsVC.isBrowseAll = self.isBrowseAll

if !isBrowseAll {
patientPaymentDetailsVC.patientPayoutDetails = self.selectedPatientPayment
patientPaymentDetailsVC.currentRemittance = self.currentRemittance
patientPaymentDetailsVC.doctorNumber = self.doctorNumber

}
}
}
}


func getPatientList() {
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.show(withStatus: "Retrieving Patient List")

APIService.PatientList.getPatientList(doctorNumber: doctorNumber, periodId: currentRemittance.periodId) { (patientListArray, error) in
guard let patientListPerPayout = patientListArray, error == nil else {
if let networkError = error {
switch networkError {
case .noRecordFound:
let alertController = UIAlertController(title: "No Record Found", message: "You don't have current payment remittance", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
case .noNetwork:
let alertController = UIAlertController(title: "No Network", message: "\(networkError.rawValue)", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
default:
let alertController = UIAlertController(title: "Error", message: "There is something went wrong. Please try again", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
}
}
SVProgressHUD.dismiss()
return
}

self.patientList = patientListPerPayout
self.latestCreditedAmountTableView.reloadData()
SVProgressHUD.dismiss()
return

}
}

**getPerPatientPAyoutDetails(from: String) function**

func getPerPatientPayoutDetails(from: String) {
SVProgressHUD.setDefaultMaskType(.black)
SVProgressHUD.showInfo(withStatus: "Retrieving Patient Details")

APIService.PatientList.getPatientDetailsPerPayout(periodId: currentRemittance.periodId, doctorNumber: doctorNumber, parameterName: .selectedByHospitalNumber, hospitalNumber: from) { (patientPayout, error) in

guard let patientPerPayoutDetails = patientPayout, error == nil else {
if let networkError = error {
switch networkError {
case .noRecordFound:
let alertController = UIAlertController(title: "No Record Found", message: "You don't have current payment remittance", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
case .noNetwork:
let alertController = UIAlertController(title: "No Network", message: "\(networkError.rawValue)", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
default:
let alertController = UIAlertController(title: "Error", message: "There is something went wrong. Please try again", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
}
}
SVProgressHUD.dismiss()
return
}
self.selectedPatientPayment = patientPerPayoutDetails
print(self.selectedPatientPayment)

SVProgressHUD.dismiss()
return
}
}

基于gePatientList()函数,它只会提取患者的完整列表。我不知道如何将所选患者的数据传递给另一个 VC。希望你能帮我。非常感谢。

可能有助于理解我的代码流程的代码

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

switch indexPath.section {
case 0: break
case 1: let selectedpatient = patientList[indexPath.row].hospitalNumber
print(selectedpatient!)
self.isBrowseAll = false
getPerPatientPayoutDetails(from: selectedpatient!)
default: break
}
}

下面是另一个 View Controller ,它将显示patentNamehospitalNumber

PatientPaymentDetailsVC

class PatientPaymentDetailsViewController: UIViewController {

@IBOutlet weak var patientProcedureTableView: UITableView!
@IBOutlet weak var hospitalNumberLabel: UILabel!
@IBOutlet weak var patientNameLabel: UILabel!

var currentRemittance: CurrentRemittance!
var doctorNumber: String!
var isBrowseAll: Bool!
var patientList: [Patient]!

var patientPayoutDetails: [PatientPayoutDetails]!

override func viewDidLoad() {
super.viewDidLoad()

setupPatientInfo()

}

//MARK: FUNCTION

func setupPatientInfo() {
self.patientNameLabel.text = patient.patientName
self.hospitalNumberLabel.text = patient.hospitalNumber
}

didselect中的getPerPatientPayoutDetails函数中拉取的数据将显示在PatientPaymentDetailsVC中。下面是输出,如您所见,我可以在 getPerPatientPayoutDetails 下提取数据,但 patentNamehospitalNumber 不显示数据。 enter image description here

最佳答案

首先不要从 TableView 单元格获取数据,而是从数据源获取

  • 将 Segue 连接到单元格
  • 删除整个方法didSelectRowAt
  • 当调用 prepare(for segue) 时,sender 参数是 cell
  • 从单元中获取索引路径,并将患者带到该索引路径。
  • 不要传递多个参数,而是在目标 Controller 中声明 var Patient : Patient! 属性并移交 patent 实例。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "showPatientPaymentDetailsVC",
let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPath(for: cell) else { return }

let patient = patientList[indexPath.row]
getPerPatientPayoutDetails(from: patient.hospitalNumber)

let patientPaymentDetailsVC = segue.destination as! PatientPaymentDetailsViewController
patientPaymentDetailsVC.patient = patient
patientPaymentDetailsVC.patientPayoutDetails = self.selectedPatientPayment
patientPaymentDetailsVC.currentRemittance = self.currentRemittance
patientPaymentDetailsVC.doctorNumber = self.doctorNumber
}

class PatientPaymentDetailsViewController: UIViewController {

@IBOutlet weak var patientProcedureTableView: UITableView!
@IBOutlet weak var hospitalNumberLabel: UILabel!
@IBOutlet weak var patientNameLabel: UILabel!

var currentRemittance: CurrentRemittance!
var doctorNumber = ""
var isBrowseAll = false
var patient : Patient!

var patientPayoutDetails: [PatientPayoutDetails]!

override func viewDidLoad() {
super.viewDidLoad()

setupPatientInfo()

}

//MARK: FUNCTION

func setupPatientInfo() {
self.patientNameLabel.text = patient.patientName
self.hospitalNumberLabel.text = patient.hospitalNumber
}

边注:

不要将 patentList 声明为隐式解包可选,将其声明为非可选空数组

var patientList : [Patient]()

关于ios - 如何在 swift 中从选定的 tableviewcell 中过滤数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56520073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com