如何快速从选定的tableviewcell中过滤数组?

Posted

技术标签:

【中文标题】如何快速从选定的tableviewcell中过滤数组?【英文标题】:How to filter array from selected tableviewcell in swift? 【发布时间】:2019-06-10 03:14:24 【问题描述】:

我很困惑我将如何构建关于如何从tableviewcell 中过滤所选数组的代码。下面的JSON 是tableview 的内容,显示为

[
    
    "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 中显示选中的hospitalNumberpatientName,这将显示为

这是我的 `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,它将显示patientNamehospitalNumber

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

didselectgetPerPatientPayoutDetails函数下拉取的数据会显示在PatientPaymentDetailsVC中。下面是输出,如您所见,我可以在getPerPatientPayoutDetails 下提取数据,但patientNamehospitalNumber 不显示数据。

【问题讨论】:

很难判断您遇到的问题是哪一部分。如果我理解正确,您只需要在override func prepare(for segue: UIStoryboardSegue, sender: Any?) 函数中获取所选单元格,对吗? 【参考方案1】:

首先不要从表格视图单元格获取数据,从数据源获取数据

将 segue 连接到 cell。 删除整个方法didSelectRowAtprepare(for segue 被调用时,sender 参数就是cell。 从单元格中获取索引路径并在该索引路径处获取患者。 与其传递多个参数,不如在目标控制器中声明var patient : Patient! 属性并移交patient 实例。
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

旁注:

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

var patientList : [Patient]()

【讨论】:

您好,感谢您提出的建议答案,请问要问什么,如何将那些注释掉的部分与您的答案一起包含?谢谢 我把它注释掉了,因为没有这些行应该做什么的信息。 很抱歉,如果缺少我的代码,但我已经添加了可能有助于完全理解它的代码。如果你不介意的话,你可以去看看。非常感谢。 仍有一些歧义,但我更新了答案 您好,仍然无法正常工作,PatientPaymentDetailsViewController(第二个视图控制器)内部的 patientProcedureTableView 变为空【参考方案2】:

通过符合UITableViewDelegate 来使用tableView(_:didSelectRowAt:) 方法。 获取选中的患者如下图:

selectedPatient = tableView[indexpath.row] as! [String:Any]

根据您编辑的问题,试试这个:

let patient = patientList[indexPath.row] as! Patient

【讨论】:

以上是关于如何快速从选定的tableviewcell中过滤数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何从数组中过滤多个选定值并将其从 uibutton 加载到其他 uitableview

如何将图像从 TableViewCell 传递到 UIImage

为选定的 tableviewCell 的背景颜色设置框架在 iOS 7 中工作正常但在 iOS 6 中没有

如何从筛选数据表 (DT) 的选定行中获取数据?

快速,从具有 CustomCells 的 tableViewCells 中检索更改的数据

将手指放在其他单元格上时,选定的 tableViewCell 被取消选择