无法在collectionview中快速显示所有json图像

问题描述 投票:0回答:1

我有一个UICollectionViewUIImageViewUILabel。我从JSON获取所有值(img url,标签文本)。我可以显示从json到collectionview的所有标签文本,但是无法在collectionview中显示所有json img url。

我正在使用SDWebImage在集合视图中显示图像,但是在某些collectionView图像中却显示为零,并且正在显示某些图像。为什么?请帮助我输入代码。

这是我的代码:

import UIKit
import SDWebImage
struct JsonData {
var iconHome: String?
var typeName: String?
init(icon: String, tpe: String) {
    self.iconHome = icon
    self.typeName = tpe
}
}

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITextFieldDelegate {

@IBOutlet weak var collectionView: UICollectionView!


var itemsArray = [JsonData]()
override func viewDidLoad() {
    super.viewDidLoad()

    homeServiceCall()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return itemsArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell

    let aData = itemsArray[indexPath.row]
    cell.paymentLabel.text = aData.typeName
    cell.paymentImage.sd_setImage(with: URL(string:aData.iconHome!), placeholderImage: UIImage(named: ""))

    // print("tableview collection images \(String(describing: aData.iconHome))")
    //print("tableview collection images count\(aData.iconHome?.count))")
    /*
    if let url = NSURL(string: aData.iconHome ?? "") {
        if let data = NSData(contentsOf: url as URL) {
            cell.paymentImage.image = UIImage(data: data as Data)
        }
    }*/
    return cell
}

//MARK:- Service-call

func homeServiceCall(){

    let urlStr = "https://dev.anyemi.com/webservices/anyemi/getfinancer"
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

        guard let respData = data else {
            return
        }
        guard error == nil else {
            print("error")
            return
        }
        do{
            let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
            //print("the home json is \(jsonObj)")
            let financerArray = jsonObj["financer"] as! [[String: Any]]
            print("home financerData \(financerArray)")

            for financer in financerArray {

                let id = financer["id"] as? String
                let pic = financer["icon"] as? String
                let typeName = financer["tpe"] as! String

                print("the icons \(String(describing: pic))")
                self.itemsArray.append(JsonData(icon: pic ?? "", tpe: typeName))
            }

            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
        catch {
            print("catch error")
        }

    }).resume()
}
}
ios json swift uicollectionview uiimageview
1个回答
0
投票

我更喜欢使用翠鸟,非常易于使用

© www.soinside.com 2019 - 2024. All rights reserved.