【问题标题】:How to JSON Data Downloading into TableView?如何将 JSON 数据下载到 TableView 中?
【发布时间】:2019-09-13 06:28:22
【问题描述】:

我想显示数据,但是运行时什么都显示不了

 import UIKit

class AnimalTableViewController: UITableViewController {
final let urlString = "https://doc-00-4k-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/ncme2210ej4sg6n7p7ipm3bmm6mht9s8/1568275200000/00096549938281215637/*/1aE90llwaDYEGS9Ci3nSmBUfaD75Rf6PD?e=download"


var nameArray = [String]()
var catArray = [String]()
var imgURLArray = [String]()


override func viewWillAppear(_ animated: Bool) {
    downloadJsonWithURL()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return nameArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "AnimalsCell") as! TableViewCell
    cell.nameLabel.text = nameArray[indexPath.row]
    cell.catLabel.text = catArray[indexPath.row]

    let imgURL = NSURL(string: imgURLArray[indexPath.row])

    if imgURL != nil {
        let data = NSData(contentsOf: (imgURL as URL?)!)
        cell.imgView.image = UIImage(data: data! as Data)

    }

    return cell
}

func downloadJsonWithURL() {
    let url = NSURL(string: urlString)
    URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in

        if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
            print(jsonObj.value(forKey: "animals")!)

            if let actorArray = jsonObj.value(forKey: "animals") as? NSArray {
                for actor in actorArray{
                    if let actorDict = actor as? NSDictionary {
                        if let name = actorDict.value(forKey: "name") {
                            self.nameArray.append(name as! String)
                        }
                        if let name = actorDict.value(forKey: "category") {
                            self.catArray.append(name as! String)
                        }
                        if let name = actorDict.value(forKey: "image") {
                            self.imgURLArray.append(name as! String)
                        }

                    }
                }
            }

            OperationQueue.main.addOperation({
                self.tableView.reloadData()
            })
        }
    }).resume()
}

}

【问题讨论】:

    标签: json swift uitableview


    【解决方案1】:

    使用 Dispatchqueue 代替 OperationQueue

    DispatchQueue.main.async {
    

    并创建一个包含结构的数组,而不是使用 3 个单独的数组

    struct Animal {
        let name: String
        let category: String
        let image: String
    }
    

    不要使用 NS 类

    //...
    if let animals = jsonObj.value(forKey: "animals") as? [Any] {
        for actor in animals {
            if let actorDict = actor as? [String: Any] {
                animal = Animal()
                if let name = actorDict["name"] as? String {
                    animal.name = name
                }
                if let category = actorDict["category"] as? String {
                    animal.category = category               
                }
                if let imageUrl = actorDict["image"] as? String {
                    animal.imageUrl = imageUrl
                }
                self.animalArray.append(animal)
            }
        }
    }
    
    DispatchQueue.main.async {
        self.tableView.reloadData()
    })
    

    self.animalArray 在哪里

    var animalArray: [Animal]
    

    【讨论】:

    • 我不能使用你的解决方案,如果你有空可以帮我解决这个问题吗?
    • 有什么问题?
    • 可以加Skype吗?
    • 没有。在此处描述您的问题。
    • 我没有问题,应用程序仍在运行但数据未显示在 tableview 中:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 2020-05-27
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多