0
我有以下的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
}
在print語句for循環的功能從「類」陣列提供正確的信息,但是當我模擬代碼,只有最後一個元素填滿每一行。
相反 '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()
很難說出你想要做什麼,但是你不應該在你的表視圖數據源方法中使用任何異步方法。這些方法應該只代表你已有的數據。好像你可以用'cell.textLabel?.text =「\(classes [indexPath.row] .section!)替換整個'getTime'塊'' –
哦,這很有效!非常感謝你! –