tableView data source empty in viewDidLoad() - Swift

前端 未结 3 1171
北恋
北恋 2021-01-14 23:31

Below is my ViewController code. The println in GetRequest prints the correct data that it receives from the HTTP GET request. At this point, tableData has 10 key-value pair

3条回答
  •  甜味超标
    2021-01-14 23:59

    The problem is that GetRequest runs asynchronously. So you have to reloadData inside the completionHandler of dataTaskWithURL:

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

    You can remove the reloadData from within viewDidLoad.

提交回复
热议问题