我定义了以下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语句从'classes'数组中提供了正确的信息,但是当我模拟代码时,只有最后一个元素填充了每一行。
我希望它显示的不是“ 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()
最佳答案
很难说出您到底想做什么,但是您不应该在表视图数据源方法中使用任何异步方法。这些方法仅代表您已经拥有的数据。似乎您可以只用getTime
替换整个cell.textLabel?.text = "\(classes[indexPath.row].section!)"
块。