Reputation: 2240
I am currently having an issue with UICollectionViewCell and when it gets loaded.
Here is the link to the video to see it in action
Below is my code in viewDidLoad i call
retrieveUrls { (success) in
if success {
self.filterImageLabel(handler: { (success) in
if success {
if self.spinner != nil {
self.spinner.stopAnimating()
self.spinner.isHidden = true
}
self.collectionView.reloadData()
}
})
}
}
In retrieveUrls i am parsing the Url to retrieve image URL and for filterImageLabel i set up an photoUrlArray to use with collectionView indexpath
func filterImageLabel(handler: @escaping (_ status: Bool) -> ()) {
photoUrlArray = photoUrlArray.filter {$0.label == "Large Square"}
if photoUrlArray.count > 0 {
handler(true)
}
}
Methods for CollectionView
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return photoUrlArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? PhotoCell {
cell.updateCellImage(imageUrl: self.photoUrlArray[indexPath.row].source)
return cell
}
return PhotoCell()
}
And Finally in photocell class i am setting up the image
override func prepareForReuse() {
super.prepareForReuse()
photoImg.image = nil
}
func updateCellImage(imageUrl : String) {
Alamofire.request(imageUrl).responseImage(completionHandler: { (response) in
guard let image = response.result.value else {return}
self.photoImg.image = image
})
}
I have looked at various different thread on stack-overflow. however cannot seem to resolve the issue.
Any ideas would be helpful.
Upvotes: 1
Views: 1957
Reputation: 51
Method for CollectionView is as follows:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoCell", for: indexPath) as? PhotoCell {
// ********* ISSUE IS HERE *********
let imageurl = URL(string: self.photoUrlArray[indexPath.row].source)
cell.photoImg.af_setImage(withURL: imageurl!)
return cell
}
return PhotoCell()
}
Upvotes: 2