-4

I have written a "refresh" method which should show new items in the table view but it doesn't seem to be reloading the cells properly.

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

It doesn't update the cells.

And it sticks like that, without loading the data

enter image description here

tmac99
  • 153
  • 2
  • 10
  • 3
    What dod you mean doesn't work? If `refresh()` called? Then `data_request()`? – Larme Apr 25 '16 at 12:21
  • Why are u reloading table view in dispatch_async? Have u tried without it? – Jigar Tarsariya Apr 25 '16 at 12:26
  • 2
    @tmac99 so what is your question? I think the best option would be to delete this question and think about what you actually want to ask. Include all information you know in the question. Take a look here http://stackoverflow.com/help/how-to-ask – Fogmeister Apr 25 '16 at 12:40

1 Answers1

0

Apply below code for reloading table view, i hope this will help u.

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()
})
Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38