我寫了一個「刷新」方法,它應該在表格視圖中顯示新項目,但它似乎沒有正確地重新加載單元格。Swift 2刷新表格視圖內容
func refresh(sender:AnyObject) {
page = 1
self.data_request()
}
func data_request(){
let user_id = Data[0].valueForKey("user_id") as? String!
let url:NSURL = NSURL(string: "http://..../perspective/\(user_id!)/page/"+"\(page)")!
let session = NSURLSession.sharedSession()
self.page = self.page + 1
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let task = session.dataTaskWithRequest(request) {
(let data, let response, let error) in
guard let _:NSData = data, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
var json: AnyObject?
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
}
catch {
return
}
guard let data_array = json as? NSArray else {
return
}
for i in 0..<data_array.count
{
if let add = data_array[i] as? NSDictionary
{
self.obj.append(Obj(data:add))
}
}
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.tableView.reloadData()
})
}
task.resume()
}
它不更新單元格。
而且它堅持這樣,無需加載數據
什麼DOD你的意思是不工作?如果'refresh()'被調用?那麼'data_request()'? – Larme
爲什麼你在dispatch_async中重載表視圖?你有沒有嘗試過? –
@ tmac99所以你的問題是什麼?我認爲最好的選擇是刪除這個問題並思考你真正想問什麼。包括你在問題中知道的所有信息。看看這裏http://stackoverflow.com/help/how-to-ask – Fogmeister