Swift 2刷新表视图内容

时间:2016-04-25 12:18:42

标签: ios swift2 pull-to-refresh

我编写了一个“刷新”方法,该方法应该在表格视图中显示新项目,但它似乎没有正确地重新加载单元格。

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

它不会更新单元格。

它坚持这样,不加载数据

enter image description here

1 个答案:

答案 0 :(得分:0)

应用以下代码重新加载表格视图,我希望这会对你有帮助。

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in

      self.tableView.reloadData()                      
})

OR

dispatch_sync(dispatch_get_main_queue(), { () -> Void in   

      self.tableView.reloadData()
})