[英]ImageView and Button inside CollectionView
I am building my first iOS-App using Swift and I am stuck.我正在使用 Swift 构建我的第一个 iOS 应用程序,但我被卡住了。 I have a CollectionView and inside one of the Cells there is a button and an imageView.
我有一个 CollectionView,其中一个 Cells 中有一个按钮和一个 imageView。 What should happen is: I click the button and the imageView shows another picture.
应该发生的情况是:我单击按钮,imageView 显示另一张图片。 I already created a CellSubClass but I just don't get how to use it.
我已经创建了一个 CellSubClass 但我不知道如何使用它。 Can anyone PLEASE help me?
谁能帮帮我吗?
Write an action method for your button inside your cellClass like this:为您的 cellClass 中的按钮编写一个操作方法,如下所示:
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).不要忘记在单元格的初始化方法中将此方法“分配”给您的按钮(当单元格启动时也调用
awakeFromNib
或者oyu 可以编写您自己的自定义初始化方法)。 You can also do it by using IBAction from storyboard (Simple dragging and clicking).您也可以通过使用情节提要中的 IBAction(简单的拖动和单击)来实现。
But programmatically it is done Like this if you use the awakeFromNib
approach:但以编程方式,如果您使用
awakeFromNib
方法,它是这样完成的:
override func awakeFromNib() {
super.awakeFromNib()
yourButton.addTarget(self, action: "changeImage", forControlEvents: UIControlEvents.TouchUpInside)
}
To learn how to create IBAction
using storyboard, read here .要了解如何使用故事板创建
IBAction
,请阅读此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.