Fill a tableview from JSON with Swift - json

I'm a total swift novice and even the simplest things I can do. For example, I would like to show in a UItableview a list of names that come from a JSON file, but I am not able to do so.
Specifically, my JSON returns the following:
[{"id_centro":"3","0":"3","nombre":"Colegio Vistarreal","1":"Colegio Vistarreal"},{"id_centro":"1","0":"1","nombre":"IES ITC Sistemas","1":"IES ITC Sistemas"}]
And I have implemented my UITableViewController in the following way:
import UIKit
class TCentrosController: UITableViewController {
var centrosList = [String] ()
override func viewDidLoad() {
super.viewDidLoad()
Dologin()
}
func Dologin(){
let url = URL (string: "https://neton.es/WS_neton/lista_centros.php")
let session = URLSession.shared
let request = NSMutableURLRequest (url: url!)
request.httpMethod = "POST"
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
guard let _:Data = data else{
return
}
do{
let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [[String:Any]]
if json.isEmpty{
DispatchQueue.main.async {
let alertController = UIAlertController(title: "Atención", message: "No hay centros disponibles", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "OK", style: .default)
{ (action ) -> Void in
print("Centros no operativos")
}
alertController.addAction(yesAction)
self.present(alertController, animated: true, completion: nil)
}
return
//Si el json no está vacío, sigue por aquí
}else{
let nombreCentro:String = json[1]["nombre"] as! String
self.centrosList.append(nombreCentro)
}
}
catch{
print("Error")
}
})
task.resume()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return centrosList.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "centros_lista", for: indexPath) as! CeldaCentrosTableViewCell
cell.labelCell.text = centrosList[indexPath.row]
return cell
}
}
But it does not return anything to me by screen. In fact, if I do a print (centrosList.count) it tells me that zero for five times.
As you can see, I do not have much idea and I sure have more than one fault that I am not able to see. By the way, I'm programming with swift 3
Thanks, any help is appreciated

It seems that you are not reloading the data to tableView on received it from server.
The general steps to be followed is
set delegate, dataSource for the tableView.
Implement all the required delegate and dataSource methods.
fetch the data from the server and store it to array
reload the tableView.
So, you should add this line below
self.centrosList.append(nombreCentro)
Add this
DispatchQueue.main.async {
self.tableView.reloadData()
}
Try and share the results.

Related

UITableView returning nil when trying to call reloadData()

I am trying to get my table view to show json data from the news api. I've been able to parse the data and display it to the console but a nil value was caught in the self.tableview.reload(). I need help in resolving the issue
let urlRequest = "https://newsapi.org/v2/everythingq=Coronavirus&sortBy=publishedAt&apiKey"
var articles: [Articles]? = []
#IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
retriveData()
}
func retriveData(){
guard let aritcleUrl = URL(string: urlRequest) else {
return
}
let request = URLRequest(url: aritcleUrl)
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
if error != nil {
print(error ?? 0)
return
}
if let data = data {
self.articles = self.parseData(data: data)
// Reload table view
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
})
task.resume()
}
func parseData(data:Data)-> [Articles] {
var articles: [Articles]? = []
do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
let jsonArticles = jsonResult?["articles"] as? [AnyObject] ?? []
for jsonArticle in jsonArticles{
let article = Articles()
article.author = jsonArticle["author"] as? String
article.title = jsonArticle["title"] as? String
article.publishedAt = jsonArticle["publishedAt"] as? String
articles?.append(article)
}
print(jsonArticles)
} catch {
print(error)
}
return articles ?? []
}
It seems that your class doesn't conform to table view's protocols.
You should edit your declaration to:
class <YourViewController>: UIViewController, UITableViewDelegate, UITableViewDataSource {
then in the viewDidLoad you should set the delegate and datasource to your tableview
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
retriveData()
}
Then you have to add the protocol stubs as suggested by xcode
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
self.articles?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// YOUR CELL CUSTOMIZATION GOES HERE
}
However I suggest you to look at any TableView tutorial on Internet, such as the Apple's official one
here

Swift: Array Struct Image not visible in TableView

me again with a new Question.
I´m gathering Picture Data from some JSON and fill it in an Array with a Struct as Datatype.
Everything works fine but when I try to display it in a TableView the Image is Missing - rest of Data is Displaying as wanted.
But the Images seems to store in the Array too (See print after guard ---new pic added---).
Also Dispatch Group is completing with "All Done".
Would be great if you could take a look and tell me what I´m doing wrong.
Array:
var memeRawData: [MemeRawData] = []
Struct:
struct MemeRawData {
let name: String?
let url: String?
let img: UIImage?
}
Dispatch Group to gather Data from JSON and Download Image and append it to array:
var meme = MemeRawData(name: "", url: "", img: UIImage())
var memeName = ""
var memeURL = ""
var memeIMG = UIImage()
let g = DispatchGroup()
MemeAPI.requestAPIImageData { (imgData, error) in
imgData?.data.memes.forEach{
memeData in
g.enter()
memeName = memeData.name!
memeURL = memeData.url!
MemeAPI.requestAPIImageFile(url: URL(string: memeData.url!)!) { (image, error) in
guard let image = image else {
print("PIC IS NIL")
return
}
memeIMG = image
print("-----NEW PIC ADDED-------")
}
meme = MemeRawData(name: memeName, url: memeURL, img: memeIMG)
self.memeRawData.append(meme)
g.leave()
}
g.notify(queue:.main) {
print("All done")
}
}
Function to get the Image from URL:
class func requestAPIImageFile(url: URL, completionHandler: #escaping (UIImage?, Error?) -> Void) {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
completionHandler(nil, error)
return}
let downloadedImage = UIImage(data: data)
completionHandler(downloadedImage, nil)
}
task.resume()
}
And to complete it tableView dequeue:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "memeAPICell")
cell?.textLabel?.text = memeRawData[indexPath.row].name
cell?.imageView?.image = memeRawData[indexPath.row].img
return cell!
}
Like I said JSON Parsing is working fine since I can display all URL´s and Names - but there seems to be a Problem with the Images. Also Console is not Printing out any Error.
Thanks in advance!
You need to set the image in main thread,
write this function,
func setImage(imageView:UIImageView,url:URL){
let task = URLSession.shared.dataTask(with: url){ (data, response, error) in
if error != nil{
return
}
DispatchQueue.main.async {
let img = UIImage(data: data!)
imageView.image = img
}
}
task.resume()
}
then call it in cellForItemAt
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "memeAPICell")
cell?.textLabel?.text = memeRawData[indexPath.row].name
self.setImage(imageView:cell!.imageView!, url: URL(string: memeRawData[indexPath.row].url)!)
return cell!
}
Set up your data array as you are already, and then for each element, download the image. it won't be available the first time you show the tableview, but that's ok. You have options for how you show that something is coming, here's a simple way to do it.
override func viewDidLoad() {
super.viewDidLoad()
// set up your meme data as you do already
// go get images in the background
for index in 0...memeRawData.count-1 {
print("Go get data for \(index)")
getImage(index: index, url: URL(string: memeRawData[index].url ?? "")!)
}
}
func getImage(index: Int, url:URL){
let task = URLSession.shared.dataTask(with: url){ (data, response, error) in
if error != nil{
return
}
DispatchQueue.main.async {
print("ready to reload /(index)")
// now that you have the image, add it to your data array
self.memeRawData[index].img = UIImage(data: data!)
// now reload the specific row in your tableview
self.tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
}
task.resume()
}
then all you have to do is modify the way you display the tableview cell. In this example, if there's no image available, I have changed the text display, but you could replace the image with a picture to show waiting, and if you have that included in your assets, you can have it available to display.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "memeAPICell")
if memeRawData[indexPath.row].img == nil
{
cell?.textLabel?.text = memeRawData[indexPath.row].name! + " awaiting image ..."
}
else
{
cell?.textLabel?.text = memeRawData[indexPath.row].name ?? "title"
}
cell?.imageView?.image = memeRawData[indexPath.row].img
return cell!
}

Swift 4, Json data doesn't appear in cells

I can't put data to the cells, I searched in different tutorials and it should work , I checked in debug area and data are downloaded but doesn't exist in cells, I tried also with custom cells but it doesn't work too. I have not got any error message, simply empty cells. Do you know maybe what can cause this issue? I spended much time for searching solution but I can't find anything, on every tutorial people do this similar to me.
struct Country: Decodable {
let name: String
}
class TableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var countries = [Country]()
var liczba = Int()
#IBOutlet var tv: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
json() {
self.tv.reloadData()
}
tv.dataSource = self
tv.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return liczba
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = countries[indexPath.row].name
return cell
}
func json (completed: #escaping()->()) {
let jsonUrl = "https://restcountries.eu/rest/v2/all"
let url = URL(string: jsonUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
do {
self.countries = try JSONDecoder().decode([Country].self, from: data!)
let numer = self.countries.count
self.liczba = numer
}
catch {
print("error")
}
}.resume()
}
}
It seems that you are having a problem with the sequentiality of your code. You are calling reloadData on your tableview but at that moment you don't have set the datasource yet.
try this:
tv.dataSource = self
tv.delegate = self
json() {
self.tv.reloadData()
}
You are never calling the completed closure of your json method, so self.tv.reloadData() will never be executed.
Add completed() after self.liczba = numer.
Since you are not returning anything from the closure I recommend to delete the completion handler and reload the table view on the main thread within the closure.
And don't forget to handle a potential error
func json() {
let jsonUrl = "https://restcountries.eu/rest/v2/all"
let url = URL(string: jsonUrl)
URLSession.shared.dataTask(with: url!) { [unowned self] (data, response, error) in
if let error = error { print(error); return }
do {
self.countries = try JSONDecoder().decode([Country].self, from: data!)
self.liczba = self.countries.count
DispatchQueue.main.async {
self.tv.reloadData()
}
}
catch {
print("error")
}
}.resume()
}
and call the method in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
tv.dataSource = self // I'd connect datasource and delegate in Interface Builder
tv.delegate = self
json()
}

No Data in array github Api Swift

How do I get the "names" from the if let statement into my tableview? The code triggers the else block right now. I am trying to parse the name data from the github api. Here's the code:
import UIKit
import Foundation
class ViewController: UITableViewController {
var tableArray = [String] ()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
parseJSON()
}
func parseJSON () {
let url = URL(string: "https://api.github.com")
let task = URLSession.shared.dataTask(with: url!) {(data, response, error ) in
guard error == nil else {
print("returned error")
return
}
guard let content = data else {
print("No data")
return
}
guard let json = (try? JSONSerialization.jsonObject(with: content,
options: JSONSerialization.ReadingOptions.mutableContainers)) as?
[String: Any] else {
print("Not containing JSON")
return
}
if let array = json["name"] as? [String] {
self.tableArray = array
} else {
print("Name is blank")
}
print(self.tableArray)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
task.resume()
}
}
extension ViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = self.tableArray[indexPath.row]
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableArray.count
}
}
Right now it's displaying "name is blank" in the console. I am trying to get the names of the users and display them in a tableview. Other url's seem to work, but I can't seem to figure out Github. Thanks for the help.
Your mistakes is.
1. Used wrong url.
2. Wrong mapping response
https://api.github.com Return Api list.
https://api.github.com/users/ Return user list.
Function fetchNameOfUsers
1. Implement request by use "user list" url.
2. Mapping the response by use "user list" structure.
func fetchNameOfUsers() {
guard let url = URL(string: "https://api.github.com/users") else {
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
guard error == nil else {
return // Error
}
guard let data = data, let _ = response else {
return // No data
}
guard let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] else {
return // Not [String: Any] list
}
let nameOfUsers = json.compactMap {
$0["login"] as? String
}
let displayedNameOfUsers = nameOfUsers.joined(separator: ", ")
print(displayedNameOfUsers)
}.resume()
}
To see result call function fetchNameOfUsers.
fetchNameOfUsers()

swift 3.0: tableview cannot show data which received as JSON through web api

i started to learn IOS development using swift 3.0.i built a simple app to call web api to query data from server database. i can get the json data and parsed it into string array. the App can print the array, but it cannot show in the tableview. it confused me several days and i searched some examples and answers on internet but still couldn't work out it.
My codes as below:
class LocationTableViewController: UITableViewController {
var names: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
//——————————————————————————get the data from web api and using json parsing————————————————————————
let config = URLSessionConfiguration.default // Session Configuration
let session = URLSession(configuration: config) // Load configuration into Session
let url = URL(string: "http://XXXXXXX/api/mylocations")!
let task = session.dataTask(with: url, completionHandler: {
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
do {
var jsonResult: NSMutableArray = NSMutableArray()
let jsonArray = try JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
jsonResult = jsonArray.mutableCopy() as! NSMutableArray
var jsonElement: NSDictionary = NSDictionary()
for i in 0..<jsonResult.count {
jsonElement = jsonResult[i] as! NSDictionary
if let name = jsonElement["Name"] as? String
{
// print(id)
// print(name)
// print(address)
// print(latitude)
// print(longitude)
// print("-------")
self.names.append(name)
}
// self.tableView.reloadData()
// print(self.names)
}
print(self.names)
// can print the string array data like [“name1”,”name2”,”name3”]
} catch {
print("error in JSONSerialization")
}
}
})
task.resume()
//-------------- ——— result is [] it seems the above code didn't put the string array to names.——————————————
print(self.names)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count;
}
internal override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) ->
UITableViewCell {
let cellIdentifier = "cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for:
indexPath as IndexPath) as UITableViewCell
// Configure the cell...
cell.textLabel?.text = names[indexPath.row]
return cell
}
}
Can anyone help me have a look?
Put self.tableView.reloadData() after print print(self.names).
At the point where you have commented...
result is [] it seems the above code didn't put the string array to
names
This line of code is being executed before the data has been downloaded within the completion handler, so we wouldn't expect to see anything here. You will note that it is working on the other print that you have within the completion handler.
The tableView.reloadData() at the end of the completion handler should be working.
Are you sure that you have the delegates set up correctly for the tableView? What do you see if you comment out the download task, and simply set
names = ["Tom", "Dick", "Harry"]
within viewDidLoad ? If that doesn't work, it's a problem with the delegates.