繁体   English   中英

从json到CollectionView自定义单元swift4的图像数组

[英]array of images from json in to the CollectionView Custom cell swift4

我想将数据从JSON Url传递到集合视图单元格,因此在解析json之后,我得到了URL链接数组,问题是如何将其发送到单元格imageView?

这是我的代码

import UIKit

class ItemsListViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

  var myItems = [Item]()


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

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell

    cell.itemPriseLabel.text = myItems[indexPath.row].currentPrice

    let imageUrl =   myItems[indexPath.row].productImageUrl
    print(imageUrl)


    cell.itemImage.image? = imageUrl[indexPath.row]

    return cell
  }

  var category1: Categories?

  @IBOutlet weak var colectionViewVariable: UICollectionView!

  override func viewDidLoad() {
    super.viewDidLoad()
    downloadJSON3 {
        self.colectionViewVariable.reloadData()

    }

    colectionViewVariable?.dataSource = self
    colectionViewVariable?.delegate = self

    // Do any additional setup after loading the view.
  }

}
func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url) { data, response, error in
        completion(data, response, error)
    }.resume()
}

func downloadImage(url: URL) {
    print("Download Started")
    getDataFromUrl(url: url) { data, response, error in
        guard let data = data, error == nil else { return }
        print(response?.suggestedFilename ?? url.lastPathComponent)
        print("Download Finished")
        DispatchQueue.main.async() {
            self.imageView.image = UIImage(data: data)
        }
    }
}

复制:在这里找到答案我已经将其堆积成一个简洁的答案,但是您肯定应该阅读链接中的答案,因为它更加完整,更好。

func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath)-> UICollectionViewCell {让单元格= collectionView.dequeueReusableCell(withReuseIdentifier:“ CollectionViewCell”,for:indexPath)为! CollectionViewCell

    cell.itemPriseLabel.text = myItems[indexPath.row].currentPrice

    if let imageUrl = myItems[indexPath.row].productImageUrl.first {

        URLSession.shared.dataTask(with: imageUrl, completionHandler: { (data, response, error) in
            guard let data = data, response != nil, error == nil else {return}


            DispatchQueue.main.async(execute: { () -> Void in
                let image = UIImage(data: data)

                if let cell = collectionView.cellForItem(at: indexPath) as? CollectionViewCell {
                    cell.itemImage.image = image
                }

            })
        }).resume()

    }

    return cell
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM