0

I want to change the url of an nsurlsession call on click of a button and refresh the tableView immediately here is what i have so far

    @IBAction func d(sender: AnyObject) {

    url = NSURL(string: "http://www.json-generator.com/api/json/get/coDxrSkrNe?indent=2")!

    NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
        if error != nil {
            print(error!)
        } else {
            do {
                if let restaurants = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [[String:AnyObject]] {



                    let names = restaurants.flatMap { $0["preferred_name"] as? String }
                    let time = restaurants.flatMap { $0["cooking_time"] as? String }


                    for restaurant in restaurants {
                        if let name = restaurant["preferred_name"] as? String {
                            let cookingTime = ""
                            let food = Food(name: name, time:cookingTime)
                            self.food.append(food)
                        }
                    }



                    dispatch_async(dispatch_get_main_queue()) {
                        self.tableView.reloadData()

                    }

                    print(names)
                }
            } catch let error as NSError {
                print(error)
            }
        }
        }.resume()

    self.tableView.reloadData()


}

Here is what my code looks like now but the tableView is still not reloading or changing the data after clicking the button

Shaggy1_0
  • 13
  • 5

1 Answers1

0

https://github.com/harsh62/stackoverflow_insert_dynamic_rows

I have created a dummy project, click on refresh and see that the data is added to the table View. I have used the same function which you created but added to my own data source. The only new thing i had to do was to allow arbitrary loads for the project.

If your app (a third-party web browser, for instance) needs to load arbitrary content, Apple provides a way to disable ATS altogether, but I suspect it’s wise for you to use this capability sparingly:

<key>NSAppTransportSecurity</key>
<dict>
  <!--Include to allow all connections (DANGER)-->
  <key>NSAllowsArbitraryLoads</key>
      <true/>
</dict>

If you are facing something like this then here is another place which you can checkout Transport security has blocked a cleartext HTTP

Reinitialize the array like this.

self.names = [];
for restaurant in restaurants {
    if let name = restaurant["preferred_name"] as? String {
        self.names.append(name)
    }
}
Community
  • 1
  • 1
Harsh
  • 2,852
  • 1
  • 13
  • 27
  • lol.. sorry about that. forgot to push the changes. Done now. – Harsh Aug 11 '16 at 02:22
  • It is inserting new rows i want to replace it with the new content not add the new content – Shaggy1_0 Aug 11 '16 at 02:29
  • If you check i am inserting it deliberately to show you that the tableView is reloading itself. – Harsh Aug 11 '16 at 02:30
  • So how can i get this to replace the data there before – Shaggy1_0 Aug 11 '16 at 02:31
  • In the sample project, I have used `self.names.append(name)`. Instead of this, create a new `Array`, add your data coming from the server and then assign it to `self.food` in your case. – Harsh Aug 11 '16 at 02:34
  • If i want to remove the old array and replace with the new one – Shaggy1_0 Aug 11 '16 at 02:38
  • Yeah. it's pretty straight forward. May be the table view is reloading itself but there is no new data to see. – Harsh Aug 11 '16 at 02:39
  • Ok so now each time i click the button new data is poped to the bottom of the table but what i want is to remove the old data and replace it with the new ones coming in – Shaggy1_0 Aug 11 '16 at 02:43
  • ` self.names = []; for restaurant in restaurants { if let name = restaurant["preferred_name"] as? String { self.names.append(name) } }` Use it like this. reinitialize the array. – Harsh Aug 11 '16 at 02:48
  • How can i contact u if i have any issue – Shaggy1_0 Aug 11 '16 at 02:52
  • Great. You can accept the answer and close this thread. Lots of comments. And yeah you can contact me on my email. harsh.batth@gmail.com – Harsh Aug 11 '16 at 02:54
  • I just followed u on github – Shaggy1_0 Aug 11 '16 at 02:55
  • Am unable to accept answer seems my reputation is too low – Shaggy1_0 Aug 11 '16 at 02:57