简体   繁体   English

Swift,如何在一个块中添加func

[英]Swift, How to add func in a block

Currently, I am working on loading data on a expandable tableview. 目前,我正在加载可扩展表格视图上的数据。 The problem is that, the loading of tableview is on ViewDidLoad(). 问题是,tableview的加载在ViewDidLoad()上。 And I have write a func to load data from server.The main thread is used for loading tableview and the second thread is running for collecting data from server. 我写了一个func从服务器加载数据。主线程用于加载tableview,第二个线程正在运行以从服务器收集数据。 It means that when the tableview is loaded, it cannot set the tableview with data I retrieved from server.Also, I tried print out the value on jsonResponse, it was correct. 这意味着当加载tableview时,它无法使用从服务器检索到的数据来设置tableview。此外,我尝试在jsonResponse上打印出该值,这是正确的。 While the value in ConfigTable() was incorrect. 虽然ConfigTable()中的值不正确。 Any idea? 任何想法? Thanks 谢谢

//keep specific symptons
  var npcPatient = npc_participant()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    fetchData(patientID)
    configTable()
    // 1 for default expand, 0 for collapse
    arrayForBool = ["1","0","0"]
    sectionTitleArray = ["DETAILS for PATIENTS","ENROLMENT INFORMATION","GENETIC DIAGNOSIS"]
    var string1 = sectionTitleArray .objectAtIndex(0) as? String
    sectionContentDict.setValue(self.DicPatientInfo, forKey: string1!)
    string1 = sectionTitleArray.objectAtIndex(1) as? String
    sectionContentDict.setValue(self.DicEnrollInfo, forKey:string1! )
    string1 = sectionTitleArray.objectAtIndex(2) as? String
    sectionContentDict.setValue(self.DicPatientInfo, forKey: string1!)

    self.myTable.registerNib(UINib(nibName: "ExpandingTableViewCell", bundle: nil), forCellReuseIdentifier: "Cell")

}

 func fetchData(id:Int){
    let cn = connection()
    let id = self.current_patient.id
    let url = "http://localhost:3000/api/v1/patients/"+String(id)
    print(url)
    // Call function to connect server by API with specific url
    cn.connectServer(url) { (jsonResponse) in
        //Parsing JSON file
    for item in jsonResponse["patients"].arrayValue{
        //get user info
    self.npcPatient.baby_symptoms = item["enrollable"]["baby_symptoms"].intValue 
    }}

  func configTable(){
    // Configue List1
  let name = current_patient.registrant_first_name + " "+current_patient.registrant_last_name
    let dob = current_patient.date_of_birth
    var gender = ""
    if(current_patient.gender == 0){
         gender = "male"
    }else{
         gender = "female"
    }
    self.DicPatientInfo.setValue(name, forKey: "PatientName")
    self.DicPatientInfo.setValue(dob, forKey: "Date_of_Birth")
    self.DicPatientInfo.setValue(gender, forKey: "Gender")    
    //config List2
    self.DicEnrollInfo.setValue(self.npcPatient.baby_symptoms,     forKey: "Did you have prolonged jaundice, liver problems, or an enlarged liver and/or spleen as a baby?")
      print(self.npcPatient.baby_symptoms)

}
typealias Completion = (success:Bool) -> Void

    func fetchData(id:Int,completionHandler: Completion) {
        let cn = connection()
        let id = self.current_patient.id
        let url = "http://localhost:3000/api/v1/patients/"+String(id)
        print(url)s
        // Call function to connect server by API with specific url
        cn.connectServer(url) { (jsonResponse) in
              //Parsing JSON file
              if let JSON = jsonResponse {
                  for item in JSON["patients"].arrayValue {
                      //get user info
                     self.npcPatient.baby_symptoms = item["enrollable"]["baby_symptoms"].intValue 
                  }
                  completionHandler(success:true)
              } else {
                  completionHandler(success:false)
              }
        }
    }

To use it in your case: 在您的情况下使用它:

fetchData(patientID, { (success) -> Void in
    // When download completes,control flow goes here.
    if success {
        // download success
        configTable()
        self.myTable.reloadData() 
    } else {
        // download fail
        print("Download problem..")
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM