【发布时间】:2015-08-23 17:06:01
【问题描述】:
我正在尝试在我调用以填充我的UITableView 的异步请求之后执行tableView.reloadData() 调用。该表会在 15-20 秒后显示数据,除非我手动开始滚动,在这种情况下它会立即加载。所以数据肯定在那里,只是没有正确加载。我该怎么做才能解决这个问题?
var jsonLoaded:Bool = false {
didSet {
if jsonLoaded {
tableView.reloadData()
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.googleapis.com/youtube/v3/search?part=id&q=\(searchTerm)&maxResults=1&key=AIzaSyD7PxAoq0O5hUxx775l_E_mnowlU4cUfcI")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
if error != nil {
println(error)
} else {
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
if let items = jsonResult["items"] as? NSArray {
for songs in items {
if let id = songs["id"] as? NSDictionary {
if let videoId = id["videoId"] as? String {
let newURL = NSURL(string: "https://www.googleapis.com/youtube/v3/search?relatedToVideoId=\(videoId)&part=snippet&type=video&maxResults=6&key=AIzaSyD7PxAoq0O5hUxx775l_E_mnowlU4cUfcI")
let newTask = NSURLSession.sharedSession().dataTaskWithURL(newURL!, completionHandler: {data, response, error -> Void in
if error != nil {
println(error)
} else {
let newJsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary
if let info = newJsonResult["items"] as? NSArray {
for videos in info {
if let snippet = videos["snippet"] as? NSDictionary {
if let title = snippet["title"] as? String {
self.recommendedTitles.append(title)
self.jsonLoaded = true
}
}
}
}
})
newTask.resume()
}
}
}
jsonLoaded = false
}
}
}
})
}
}
【问题讨论】:
-
谁是你的表的数据源?
-
一切尽在
class: SongViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {}
标签: ios uitableview swift reload