0

I am new to Swift, and am trying to create a table that reads JSON data from my website. I followed some tutorials, and was able to get my code to work with a table controller. Now I'm trying to use a view controller with a table view inside, so I can have more customization. My problem is, I can't get the data to actually show up when I try to use my new code.

This is what I have in my viewController.swift:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
var TableData:Array< String > = Array < String >()


override func viewDidLoad() {
    super.viewDidLoad()

    get_data_from_url("http://www.stevenbunting.org/Alliris/service.php")
}



func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return TableData.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = TableData[indexPath.row]

    return cell
}






func get_data_from_url(_ link:String)
{
    let url:URL = URL(string: link)!
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData


    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (
        data, response, error) in

        guard let _:Data = data, let _:URLResponse = response  , error == nil else {

            return
        }


        self.extract_json(data!)


    })

    task.resume()

}


func extract_json(_ data: Data)
{


    let json: Any?

    do
    {
        json = try JSONSerialization.jsonObject(with: data, options: [])
    }
    catch
    {
        return
    }

    guard let data_list = json as? NSArray else
    {
        return
    }


    if let countries_list = json as? NSArray
    {
        for i in 0 ..< data_list.count
        {
            if let country_obj = countries_list[i] as? NSDictionary
            {
                if let country_name = country_obj["user"] as? String
                {
                    if let country_code = country_obj["friendlist"] as? String
                    {
                        TableData.append(country_name + " [" + country_code + "]")
                    }
                }
            }
        }
    }



    DispatchQueue.main.async(execute: {self.do_table_refresh()

    })

}

func do_table_refresh()
{
    self.tableView.reloadData()

}


}
  • Have you set the ViewController as the tableView's datasource? Probably not, as your ViewController-code doesn't implement the [UITableViewDataSource-protocol](https://developer.apple.com/reference/uikit/uitableviewdatasource) ;) – Nathan Mattes Mar 16 '17 at 20:16

1 Answers1

0

Probably you didn't set the tableView's dataSource. To do this, implement the UITableViewDataSource-protocol in the ViewController-class and set the tableView's dataSource-property to self in the viewDidLoad(), for example:

class ViewController: UIViewController, UITableViewDataSource {
// ...
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        // ...
    } 
    //...
}

Oh, and don't forget about the Apple Transport Security-settings, otherwise you won't see anything as iOS doesn't allow HTTP anymore, you have use HTTPS. The right way to handle this is to get an SSL-Certificate for your domain.

The quick'n'dirty and absolutely not recommended way is to disable ATS or to set an exception for certain, trustworthy domains.

Community
  • 1
  • 1
Nathan Mattes
  • 339
  • 3
  • 11