I am trying to populate a Collection View with an array of strings and an image but when the code runs the image being displayed is the initialized image not the one I pushed into the array. However, the strings are correct.
I declare my class in a separate swift file:
import UIKit
class Tiles {
var title: String?
var image: UIImage?
var display: String?
init(title: String, image: UIImage, display: String) {
self.title = title
self.image = #imageLiteral(resourceName: "blank_whiteTile_48pt")
self.display = display
}
}
And then in the Collection View populate the array and then associate the array with the cells of the Collection View:
var tiles : [Tiles] = [Tiles(title: "Ceramic", image:#imageLiteral(resourceName: "ceramic_white"), display: "All"),
Tiles(title: "Marble", image: #imageLiteral(resourceName: "marble"), display: "All")]
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TileDisplay
//define each cell
cell.tiles = tiles[indexPath.item]
return cell
}
I also tried to swap out the UIImage for UIImageView but that did not produce any different results.
In your initializer you need to do the following:
Remove the static value you assigned to the self.image. Assign your image argument to self.image (thus making it dynamic)...
You can set your argument's default value so that if no argument is entered in the initialization call you automatically use the "#imageLiteral(resourceName: "blank_whiteTile_48pt")"...
What's happening right now is you have the value hardcoded.
init(title: String,
image: UIImage = #imageLiteral(resourceName: "blank_whiteTile_48pt"),
display: String) {
self.title = title
self.image = image
self.display = display
}