gpt4 book ai didi

ios - swift : TableView

转载 作者:行者123 更新时间:2023-11-29 01:13:57 25 4
gpt4 key购买 nike

我定义了以下 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”数组的正确信息,但是当我模拟代码时,只有最后一个元素填满每一行。

Screenshot of simulator

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

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

最佳答案

很难准确地说出您要做什么,但您不应该在 TableView 数据源方法中使用任何异步方法。这些方法应该只代表您已有的数据。好像你可以替换整个 getTime只用 cell.textLabel?.text = "\(classes[indexPath.row].section!)" block .

关于ios - swift : TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35446396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com