我有一个方法,它为我解析JSON然后在闭包加载并将图像保存到磁盘。在viewDidLoad中调用此方法。然后加载表。问题是,当表加载时,它内部只有一个单元格,因为当加载表时,dataTaskWithRequest没有足够的时间下载所有信息。所以它加载一个单元格,然后如果我按下链接到函数tableView.reloadData()的按钮,则出现其他单元格。
如何使tableView等待dataTaskWithRequest并仅在从Internet上获取完成后自我显示?
这里我在viewDidLoad中调用的方法看起来像
var JSONStorage : [Article?]?
var objects = [Article?]()
var imgPath : String?
var battlesArticlesListImgPath : String?
var battleArticlesListHash : String?
var battleArticlesListArray = [ArticlesList?]()
@IBOutlet weak var tableView: UITableView!
@IBAction func button(sender: AnyObject) {
print(battleArticlesListArray)
print(battleArticlesListHash)
dump(battleArticlesListArray)
self.tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
downloadBattlesArticlesList()
func downloadBattlesArticlesList() {
let number = arc4random_uniform(1000)
let urlString = "http://78.27.190.58:3200/api/category_listing/0/0/?\(number)"
if let url = NSURL(string: urlString) {
if let data = try? NSData(contentsOfURL: url, options: []) {
var json = JSON(data: data)
self.battleArticlesListHash = json["content_version"].stringValue
let UserDefaults = NSUserDefaults.standardUserDefaults()
UserDefaults.setValue(self.battleArticlesListHash, forKey: "battleArticlesListHash")
print("preved")
for element in json["listing"].arrayValue {
let id = Int(element["id"].stringValue)
let title = element["title"].stringValue
let subtitle = element["subtitle"].stringValue
let img = element["image"].stringValue
let url = NSURL(string: img)
let urlRequest = NSURLRequest(URL: url!)
let task = NSURLSession.sharedSession().dataTaskWithRequest(urlRequest, completionHandler: {
(data, response, error) -> Void in
let image = UIImage(data: data!)
var documentsDirectory:String?
var paths:[AnyObject] = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if paths.count > 0 {
documentsDirectory = paths[0] as? String
var savePath = documentsDirectory! + "/\(self.randomStringWithLength(5)).jpg"
self.battlesArticlesListImgPath = savePath
NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)
let obj:ArticlesList = ArticlesList(id: id!, title: title, subtitle: subtitle, image: self.battlesArticlesListImgPath!)
self.battleArticlesListArray.append(obj)
}
})
task.resume()
}
}
}
print(battleArticlesListArray)
}
最终打印在执行时也会显示空数组。
答案 0 :(得分:0)
在viewDidLoad中,隐藏你的tableView:
tableView.hidden = true
只需在此处调用tableView.reloadData()
if paths.count > 0 {
documentsDirectory = paths[0] as? String
var savePath = documentsDirectory! + "/\(self.randomStringWithLength(5)).jpg"
self.battlesArticlesListImgPath = savePath
NSFileManager.defaultManager().createFileAtPath(savePath, contents: data, attributes: nil)
let obj:ArticlesList = ArticlesList(id: id!, title: title, subtitle: subtitle, image: self.battlesArticlesListImgPath!)
self.battleArticlesListArray.append(obj)
}
tableView.hidden = false
tableView.reloadData()
这是因为你的任务正在进行。一旦返回一些值,并完成所有任务,只需刷新tableview。
您打印打印空值的原因是它在返回数据之前执行。
另外,在iOS下查看有关线程的文档并不是一个坏主意: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/Introduction/Introduction.html