【问题标题】:array of images from json in to the CollectionView Custom cell swift4从 json 到 UICollectionView 自定义单元格的图像数组 swift 4
【发布时间】:2018-02-27 19:43:03
【问题描述】:

我想将数据从我的 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.
  }

}

【问题讨论】:

标签: ios swift uicollectionview


【解决方案1】:
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)
        }
    }
}

重复:找到答案HERE 我已将其汇总为一个简洁的答案,但您绝对应该阅读链接中的答案,因为它更完整、更好。

【讨论】:

    【解决方案2】:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as!集合视图单元

        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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2017-05-07
      • 1970-01-01
      相关资源
      最近更新 更多