目前,我正致力于在可扩展的tableview上加载数据。问题是,tableview的加载是在ViewDidLoad()上。我已经编写了一个func来从服务器加载数据。主线程用于加载tableview,第二个线程用于从服务器收集数据。这意味着当加载tableview时,它无法使用从server检索到的数据设置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)
}
答案 0 :(得分: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)
}
}
}
在你的情况下使用它:
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..")
}
}