Reputation: 9
I want to use the image from the placeStarsIcon array using the placeStarsCount. So instead of showing the number 5 from placeStarsCount I want to display "5stars.png" in the cell.placeStarsIcon.image
This is how i try but i get an error:Cannot subscript a value of type '[UIImage]' with an argument of type '[Int]'
:
cell.placeStarsIcon.image = placeStarsIcon[plaaceStarsCount]
Here is the code:
class FindLocalViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
let placeName = ["Place Name 0", "Place Name 1", "Place Name 2"]
let placeDescription = ["Place Description 0. Text to see if can go to more than 2 lines because we need.", "Place Description 1. Text to see if can go to more than 2 lines because we need.", "Place Description 2. Text to see if can go to more than 2 lines because we need."]
//Thats the number of stars for each placeName. 5 stars, 2 stars, 0 stars.
let placeStarsCount = [5, 2, 0]
let placeStarsIcon = [#imageLiteral(resourceName: "0stars.png") , #imageLiteral(resourceName: "1stars.png") , #imageLiteral(resourceName: "2stars.png"), #imageLiteral(resourceName: "3stars.png"), #imageLiteral(resourceName: "4stars.png"), #imageLiteral(resourceName: "5stars.png")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return placeName.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as!
FindLocalCollectionViewCell
cell.placeName.text = placeName[indexPath.row]
cell.placeDescription.text = placeDescription[indexPath.row]
/*
Here i want to use the image from the placeStarsIcon array using the placeStarsCount
So instead of showing the number 5 from placeStarsCount i want to display "5stars.png" in the cell.placeStarsIcon.image
This is how i try but i get an error:
Cannot subscript a value of type '[UIImage]' with an argument of type '[Int]'
*/
cell.placeStarsIcon.image = placeStarsIcon[placeStarsCount]
return cell
}
}
Upvotes: 0
Views: 43
Reputation: 100541
Replace
cell.placeStarsIcon.image = placeStarsIcon[placeStarsCount]
with
cell.placeStarsIcon.image = UIImage(named:"\(placeStarsCount[indexPath.row])stars.png")
and get rid of placeStarsIcon
array , btw you can have a struct instead of separate arrays like
struct Place {
let name,desc:String
let imgNumber:Int
}
Upvotes: 1