Arfaa Shariff Arfaa Shariff - 4 months ago 12
Swift Question

Adding images from within the app based on json response from url in a UICollectionView

I have a

UICollectionViewCell
that has a label and an
imageView
. I'm dynamically populating the
label
by storing the
json
response in an
array
.

Based on the response that i receive from the url I want to append an
image
to it.

For example if my label is populated with "Fridge". I want to display an
image
of fridge in my
imageView
. The images are stored in
Assets.xcassets
folder.

Any help will be appreciated. Thank you

Answer

If you store all the images in your Assets.xcassets, when you declare the cell (cellForItemAtIndexPath) and assign the data to it, you can just do something like:

cell.imageView.image = UIImage(named: YOUR_LABEL_VALUE)

If you store the images in .png format, with the same name of the labels you are gonna use, that can work.

For examples your labels are:

  • Fridge
  • Microwave
  • Sink

So your images in your Assets.xcassets:

  • Fridge.png
  • Microwave.png
  • Sink.png