gpt4 book ai didi

ios - tableView 数据源在 viewDidLoad() 中为空 - Swift

转载 作者:可可西里 更新时间:2023-11-01 00:16:44 27 4
gpt4 key购买 nike

下面是我的 ViewController 代码。 GetRequest 中的 println 打印它从 HTTP GET 请求接收到的正确数据。此时,tableData 有 10 个键值对。但是,如果我在 viewDidLoad() 中调用 GetRequest() 之后放置一个断点,tableData 为空并且 tableView 中不显示任何内容。为什么是这样?我哪里错了?

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
@IBOutlet var tableView: UITableView!
var tableData: [String:String] = [:]
let textCellIdentifier = "TextCell"

func GetRequest(urlPath: String)
{
var LatestWaitTimes: [String:String] = [:]
let url: NSURL = NSURL(string: urlPath)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in

if error != nil {
// If there is an error in the web request, print it to the console
println(error.localizedDescription)
}

var err: NSError?
var jsonResult: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err)
if err != nil {
// If there is an error parsing JSON, print it to the console
println("JSON Error \(err!.localizedDescription)")
}

let json = JSON(jsonResult!)
let count: Int? = json.array?.count

if let ct = count {
for index in 0...ct-1 {
let name = json[index]["CrossingName"].string
var wait = json[index]["WaitTime"].stringValue
if (wait == "-1")
{
wait = "No Info"
}
else
{
wait += " min"
}
println(name!+": "+wait)
LatestWaitTimes[json[index]["CrossingName"].stringValue] = wait as String?
}
}
self.tableData = LatestWaitTimes
})
task.resume()
}

override func viewDidLoad() {
super.viewDidLoad()
var apiInfo = GetWaitTimes()
GetRequest(apiInfo.BorderCrossingApi+"?AccessCode="+apiInfo.AccessCode)
self.tableView.delegate = self
self.tableView.dataSource = self
tableView.reloadData()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(self.textCellIdentifier) as! UITableViewCell
let thisEntry = self.tableData.values.array[indexPath.row]
cell.textLabel?.text = thisEntry+": "+tableData[thisEntry]!
return cell
}

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
}

最佳答案

问题是 GetRequest 异步运行。所以你必须在 dataTaskWithURLcompletionHandlerreloadData:

let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in

// do all of your existing stuff here

dispatch_async(dispatch_get_main_queue()) {
self.tableData = LatestWaitTimes
self.tableView.reloadData()
}
})
task.resume()

您可以从 viewDidLoad 中删除 reloadData

关于ios - tableView 数据源在 viewDidLoad() 中为空 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29597208/

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