简体   繁体   中英

ImageView and Button inside CollectionView

I am building my first iOS-App using Swift and I am stuck. I have a CollectionView and inside one of the Cells there is a button and an imageView. What should happen is: I click the button and the imageView shows another picture. I already created a CellSubClass but I just don't get how to use it. Can anyone PLEASE help me?

Write an action method for your button inside your cellClass like this:

func changeImage
{
     var image: UIImage = UIImage(named: "your image name")!
     yourImageView.image = image
}

Don't forget to "assign" this method to your button either in initialization method for the cell ( awakeFromNib is also called when cell is started or oyu could write your own custom init method). You can also do it by using IBAction from storyboard (Simple dragging and clicking).

But programmatically it is done Like this if you use the awakeFromNib approach:

override func awakeFromNib() {
        super.awakeFromNib()
        yourButton.addTarget(self, action: "changeImage", forControlEvents: UIControlEvents.TouchUpInside)
    }

To learn how to create IBAction using storyboard, read here .

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