priyanka gautam
priyanka gautam

Reputation: 377

Table not reload data until Scroll

I am calling webservice call . They load data in table until i am scrolling. I want reload data when response come . I am newer in Swift 2 Please help any help would be apperciated.

Here is my code:

  let url : String = String(format: "http://api.nytimes.com/svc/news/v3/content/nyt/all/720.json?api-key=%@",apiKey)

   t url1: NSURL = NSURL(string: url)!

  let session = NSURLSession.sharedSession()

    let task1 = session.dataTaskWithURL(url1, completionHandler: {
        (data, response, error) -> Void in

        do {
            let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSDictionary

            self.dict = jsonData;

            self.array1 = (self.dict.objectForKey("results") as? NSMutableArray)!
            print(self.array1)
        self.table.reloadData()

        } catch {
        }
    })
    task1.resume()


  extension ViewController1: UITableViewDelegate, UITableViewDataSource {

       }                      func tableView(tView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return self.array1.count
        }

        func tableView(tView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let identifier = "CustumCell"
            var cell: CustumCell! = tView.dequeueReusableCellWithIdentifier(identifier) as? CustumCell
            if cell == nil {
                var nib:Array = NSBundle.mainBundle().loadNibNamed("CustumCell", owner: self, options: nil)
                cell = nib[0] as? CustumCell
            }

                       cell.label.text = self.array1.objectAtIndex(indexPath.row).objectForKey("abstract") as? String

            return cell
        }

    }

Upvotes: 2

Views: 2216

Answers (2)

dhruvm
dhruvm

Reputation: 2812

What MirekE said, except updated for Swift 3, Xcode 8.2:

DispatchQueue.main.async {
    self.table.reloadData() 
}

Upvotes: 2

MirekE
MirekE

Reputation: 11555

You are running table.reloadData() from a completion handler. The chances are that the code is not executed on the main thread. All UI stuff needs to be done on the main thread. Try forcing the reloadData to run on the main thread:

dispatch_async(dispatch_get_main_queue()) {
    self.table.reloadData()
}

Upvotes: 4

Related Questions