AnaMM
AnaMM

Reputation: 317

How to download images async for collectionView inside TableViewCell?

I did a grid (collectionView) inside a tableViewCell, the problem is loading different images per cell. Make a Json like this:

{
   {
    "name": "Vegetales"
    "images": { imagesURLStrings }
   },
   {
     "name": "Frutas"
     "images": { imagesURLStrings }
   },
}

I use this page for custom the view and this other to make the async download.

I think the problem is because, when I try to defined the quantity of cells for the collectionView inside the tableviewCell, the assignation its wrong, its not working, and I don't know how to fixed.

The code for download the images:

func loadImages() {

    var section = 0
    var row = 0

    while (section < searchDataresults.count) {

        for i in searchDataresults[section].images {

            let key = section * 10 + row

            let imageUrl = i
            let url:URL! = URL(string: imageUrl)
            task = session.downloadTask(with: url, completionHandler: { (location, response, error) -> Void in
                if let data = try? Data(contentsOf: url){
                    // 4
                    DispatchQueue.main.async(execute: { () -> Void in
                        // 5
                        // Before we assign the image, check whether the current cell is visible

                        let img:UIImage! = UIImage(data: data)

                        saveImage(image: img, name: String(key))

                    })
                }
            })
            task.resume()
            row += 1
        }

        section += 1
        row = 0
    }

}

}

And the code were I put the images on the collectionView, remembering that it is inside a tableViewCell, so the quantity of cells have to change depending of the images.count of the json.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return searchDataresults[cellLoad].images.count
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellImage", for: indexPath as IndexPath)

    let key = cellLoad * 10 + indexPath.row

    if let img = loadImage(name: String(key)) {

        let imageView = UIImageView(image: img)
        imageView.frame = cell.frame
        imageView.bounds = cell.bounds
        imageView.center = cell.center

        cell.contentView.addSubview(imageView)

        print(key)

    } else {

        let imageView = UIImageView(image: UIImage(named: "emptyImg"))
        imageView.frame = cell.frame
        imageView.bounds = cell.bounds
        imageView.center = cell.center

        cell.contentView.addSubview(imageView)
    }

    return cell
}

I really appreciate your help!

Upvotes: 3

Views: 1088

Answers (1)

adarshaU
adarshaU

Reputation: 960

subclass UICollectionviewCell and reset the content of your collection view cell

override func prepareForReuse() {
super.prepareForReuse()
self.customImageview.image = nil

 }

Upvotes: 1

Related Questions