當我在我的UITableViewController
中加載JSON文件時,它會加載和更新我的數據源和視圖,但只會在我觸摸屏幕時呈現更新。Swift 2.0 iOS9 UITableView更新不會呈現
加載和解析的代碼我使用這樣的容貌:
func fetchData() {
let jsonUrl = 'http://myrestserver.com/apicall'
let session = NSURLSession.sharedSession()
let urlObject = NSURL(string: jsonUrl)
let task = session.dataTaskWithURL(urlObject!) {
(data, response, error) -> Void in
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! NSDictionary
var items :[Article] = []
let jsonItems = jsonData["channel"] as! NSArray
for (_, item) in jsonItems.enumerate() {
let article = Article()
article.id = item["id"] as? String
article.title = item["title"] as? String
article.guid = item["guid"] as? String
items.append(article)
}
self.articles.insertContentsOf(items, at: 0)
} catch {
print("error fetchData")
}
}
task.resume()
self.tableView.reloadData()
}
有沒有一種方法,我不知道的處理這種重新渲染? 我已經試過渲染這裏描述爲UITableViewCell
類似的方法: setNeedsLayout and setNeedsDisplay
但沒有運氣,有人可以解釋什麼是呈現新的記錄的最佳做法是什麼?
最好的問候, 喬斯
你是叫'reloadData'?你是否在主線程中調用它? – luk2302
你在主線程中更新你的UI嗎? – Blaszard
用相關代碼更新您的問題。 – rmaddy