使用JSON数据填充表

使用JSON数据填充表

本文介绍了使用JSON数据填充表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本地页面中的JSON填充表.到目前为止,它已从JSON提取所有数据,但仍然没有任何文本进入单元格.

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.

我检查了单元格的标识符,它与withIdentifier匹配,或者使用了变量nom或fec,也无法正常工作.

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.

任何帮助都是有用的(使用Alamofire除外).

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!
    }
}

我什至将自定义文本添加到单元格?.textLabel?.text,并且不起作用.

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

推荐答案

感谢@vadian

我有答案:

我只需要在DispatchQueue.main.async {}之间添加reloadData()

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

这篇关于使用JSON数据填充表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:00