簡體   English   中英

Swift,如何在一個塊中添加func

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

目前,我正在加載可擴展表格視圖上的數據。 問題是,tableview的加載在ViewDidLoad()上。 我寫了一個func從服務器加載數據。主線程用於加載tableview,第二個線程正在運行以從服務器收集數據。 這意味着當加載tableview時,它無法使用從服務器檢索到的數據來設置tableview。此外,我嘗試在jsonResponse上打印出該值,這是正確的。 雖然ConfigTable()中的值不正確。 任何想法? 謝謝

//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)
              }
        }
    }

在您的情況下使用它:

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