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.
reloadData()on your table view after receiving the data. WhyNSString, whyNSMutableURLRequest, whyNSURLin Swift 3?resumeline.