简体   繁体   中英

Is it possible to change image of cell without making a custom cell class?

In Xcode, if say I have a UICollectionViewCell that has a UIImageView in it, I am used to making a custom cell class to access that UIImage . However, is this necessary? I am convinced that I don't have to make a custom cell class just to access a UIImage .

Would I have to do something like...

for var subview in cell.subviews
{
    if subview is UIImageView
    {
        subview = UIImageView()
        subview.image = //change image here
    }
}

Thank you!

Never dig into the private subview structure of views. That implementation can change in any iOS update. That image view you are finding is there for other purposes.

UICollectionViewCell does not provide an image property that you can use. If you wish to show an image, create a custom cell class that contains your own UIImageView setup with your desired constraints. Use that image view, not one you happen to find by digging into the private subviews.

Provide a property in your custom cell class to make it simple to access that image view in the cellForItemAt method.

If you are seriously opposed to subclassing the UICollectionViewCell, those objects have a contentView property to which you could add an image. You could then call cell.contentView.layer.contents = someImage.cgImage and you don't then need to go mucking around with undocumented subviews.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM