Swift:tableView

我有以下tableView定义:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("ProductCell", forIndexPath:indexPath) print("Pass it to tableview \(courseName2) ") print("Pass it to tableview \(codeName2) ") var count = 0 getTime( (self.courseName2!), courseCode: (self.codeName2!), completion:{(success:[ClassSchedule]) -> Void in count = classes[0].total! print("TOTAL CKASSESSSs \(count) ") dispatch_async(dispatch_get_main_queue(),{ self.tableView.reloadData() for var i = 0; i < count; ++i { //print("FOR LOOP COUNT: \(i)") cell.textLabel?.text = "\(classes[i].section!)" // Start Time: \(classes[i].start!)End Time: \(classes[i].end!) print("FOR LOOP COUNT: \(i) Section\(classes[i].section!)") } }); }) return cell } 

for循环函数中的print语句提供了来自“类”数组的正确信息,但是当我模拟代码时,只有最后一个元素填满了每一行。

模拟器的屏幕截图

而不是“TST 101”,我希望它按顺序显示“LEC 001”,“LEC 002”,“LEC 003”,“LEC 004”,“TST 101”,它们包含在“类”数组中。

getTime函数,通过JSON调用获取数据:

  // Read the JSON do { let data: NSData? = NSData(contentsOfURL: url) if let jsonResult: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary { //////ARRAY/////// let dataArray = jsonResult["data"] as! NSArray ////// let arrayLength = dataArray.count var count = 0 //classes.append(ClassSchedule(course: item["subject"] as! String, code: item["catalog_number"] as! String, section: item["section"] as! String) ) print("TOTAL OF CLASSES: \(arrayLength)"); for item in dataArray { classes.append(ClassSchedule(course: "TEMP", code: "TEMP", section: "TEMP", days:"TEMP", start:"TEMP", end:"TEMP", building:"TEMP", room:"TEMP", total: arrayLength) ) classes[count].course = (item["subject"] as! String) classes[count].code = (item["catalog_number"] as! String) classes[count].section = (item["section"] as! String) print("Subject: \(classes[count].course!) \(classes[count].code!)"); print("Section: \(classes[count].section!)"); // print("Section: \(section_numb)"); let subjectArray = item["classes"] as! NSArray for item2 in subjectArray{ let dateDictionary = item2["date"] as! NSDictionary //let startTime = dateDictionary["start_time"]! //self.performSelectorOnMainThread("updateIPLabel:", withObject: startTime, waitUntilDone: false) //let endTime = dateDictionary["end_time"]! classes[count].days = (dateDictionary["weekdays"] as! String) classes[count].start = (dateDictionary["start_time"] as! String) classes[count].end = (dateDictionary["end_time"] as! String) string1 = classes[count].start! print("Weekdays: \(classes[count].days!)"); print("START TIME: \(classes[count].start!)"); print("End Time: \( classes[count].end!)"); let locationDictionary = item2["location"] as! NSDictionary classes[count].building = (locationDictionary["building"] as? String) classes[count].room = (locationDictionary["room"] as? String) print("Building: \(classes[count].building) \(classes[count].room)"); count += 1 print(count) print("") } } //let subject = dataArray["subject"] } } catch { print("bad things happened") } //print("ASDIAWDWD: \(classes[0].section)") // print("ASDIAWDWASDASDD: \(classes[1].section)") completion(classes: classes) }).resume() 

很难说明你想要做什么,但是你不应该在你的table view数据源方法中使用任何asynchronous方法。 这些方法应该只代表你已有的数据。 看起来像你可以只用cell.textLabel?.text = "\(classes[indexPath.row].section!)"来replace整个getTime块。