2016-05-11 11 views
2

Momentan arbeite ich an dem Laden von Daten in einem erweiterbaren Tableview. Das Problem ist, dass das Laden von TableView auf ViewDidLoad() ist. Und ich habe eine Funktion geschrieben, um Daten vom Server zu laden. Der Haupt-Thread wird zum Laden von TableView verwendet und der zweite Thread wird zum Sammeln von Daten vom Server ausgeführt. Es bedeutet, dass wenn die Tabellenansicht geladen wird, kann es die Tabellenansicht nicht mit Daten, die ich vom Server abgerufen. Außerdem habe ich versucht, den Wert auf jsonResponse auszudrucken, es war korrekt. Während der Wert in ConfigTable() falsch war. Irgendeine Idee? DankSwift, Func in einem Block hinzufügen

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

} 
+0

, nachdem die Daten zurück kommt und man es auf eine Eigenschaft speichern (die die Tabellenansicht zu füllen verwendet wird) Sie aufrufen müssen 'reloadData' in der Tabellenansicht (muss im Hauptthread aufgerufen werden) – Wain

+1

Sie rufen auch' configTable' zu ​​früh auf, sie sollte innerhalb des Callbacks in 'fetchData' aufgerufen werden. – Wain

+0

@Wain I reloadData in fecthData() Aber ich nicht weiß wie man configTable innerhalb von fetchData aufruft. Kannst du mir den Code zeigen? Thx –

Antwort

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

Um es in Ihrem Fall verwenden:

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..") 
    } 
} 
Verwandte Themen