Reputation: 11
I have come across an issue I cannot resolve.
I have a struct
and would like to change the image of a button in each collection view. My text/image works as according to plan. However, when trying to change the image button I am struggling. Could someone please help me?
class CollectionViewContent{
var caption = ""
var mainImage: UIImage!
var userProfileImage : UIButton
init(caption: String, mainImage: UIImage!, userProfileImage : UIButton! )
{
self.description = description
self.mainImage = featuredImage
self.userProfileImage = postedUserProfileImage
}
static func showPosts() -> [Posts]
{
return [
Posts(description: "Sweet", mainImage: UIImage(named: "Image 1"), postedUserProfileImage:!),
Posts(description: "Awesome", mainImage: UIImage(named: "Image 2")!),
Posts(description: "WOW!", mainImage: UIImage(named: "Image 3")!
)
]
}
}
This is my collectionViewCell class
class colViewContetCollectionViewCell: UICollectionViewCell {
var posts: Posts! {
didSet {
updateUI()
}
}
@IBOutlet weak var featuredImage: UIImageView!
@IBOutlet weak var postCaptionLabel: UILabel!
@IBOutlet weak var postedUserProfileImage: UIButton!
func updateUI() {
postCaptionLabel?.text! = posts.title
featuredImage?.image! = posts.featuredImage
postedUserProfileImage = posts.postedUserProfileImage
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = 10.0
self.clipsToBounds = true
}
}
I think this line of code is incorrect by the way, I am not too sure.
postedUserProfileImage = posts.postedUserProfileImage
Upvotes: 0
Views: 37
Reputation: 3556
Instead of:
postedUserProfileImage = posts.postedUserProfileImage
You should do:
postedUserProfileImage.setImage(posts.postedUserProfileImage.image(for: UIControlState.normal), for: UIControlState.normal)
Since postedUserProfileImage
is a UIButton
, you need to use the setImage
method to set it's image for a control specific control state. If no images are set for the other control states, the image for the normal state is used for the others too.
And since posts.postedUserProfileImage
is also a UIButton
(as mentioned in the comments below) you need to get the button's image via the image(for:)
method.
Upvotes: 1