1

I'm trying to populate a table using JSON from a local page. So far it fetches all data from the JSON but still doesn't get any text into the cells.

I checked the identifier of my cell and it matches with the withIdentifier or used the variable nom or fec and also doesn't work.

Any help would be useful (except using Alamofire).

import UIKit

class ListarViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var nom: String?
    var fec: String?
    var datas = [Actividad]()

    struct Actividad : Codable {
        let actividad: String?
        let fecha: String?
    }

    @IBOutlet weak var pruebalbl: UILabel!
    var correo: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        pruebalbl.text = correo
        //print(correo)
        let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:8080/swiftdb/listarAct.php")! as URL)

        request.httpMethod = "POST"
        let postString = "correo=\(pruebalbl.text!)"
        request.httpBody = postString.data(using: String.Encoding.utf8)

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

            guard let data = data, error == nil else{return}

            do {
                let articlesData = try JSONDecoder().decode([Actividad].self, from: data)

                DispatchQueue.main.async{
                    self.datas = articlesData

                    let aarti = self.datas
                    print(self.datas)

                    for item in aarti {
                        self.nom = item.actividad
                        self.fec = item.fecha
                        print("Nombres  \(self.nom)")
                        print("Fecha  \(self.fec)")
                    }
                }
            } catch let error as NSError {
                print(error)
            }

            print("response = \(response)")

            let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
            print("responseString = \(responseString)")
        }
        task.resume()
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        cell?.textLabel?.text = datas[indexPath.row].actividad
        return cell!
    }
}

I even to add custom text to cell?.textLabel?.text and doesn't work.

8
  • Why is this tagged PHP? Commented Mar 4, 2018 at 20:56
  • 1
    @max Updated...done? Commented Mar 4, 2018 at 20:57
  • 1
    You need to call reloadData() on your table view after receiving the data. Why NSString, why NSMutableURLRequest, why NSURL in Swift 3? Commented Mar 4, 2018 at 21:04
  • Still not working Commented Mar 4, 2018 at 21:09
  • Consider that after receiving the data is in the completion block, not after the resume line. Commented Mar 4, 2018 at 21:14

2 Answers 2

1

Thanks to @vadian

I've got the answer:

I only have to add reloadData() between my DispatchQueue.main.async{}

Sign up to request clarification or add additional context in comments.

Comments

0

Perhaps you have to validate status from url request before call your async function.

let response = response as? HTTPURLResponse
if(response?.statusCode == 200) {
  //Call your DispatchQueue
}

I hope this can will be useful for you.

Grettings!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.