【发布时间】:2017-08-06 09:44:37
【问题描述】:
每当我转到 UICollectionViewController 时,我都会尝试将图像从视图控制器传递到 UICollectionViewCell。通常,我只会使用以下代码将变量直接传递给 UICollectionViewController。
let myCollectionViewController = MyCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())
myCollectionViewController.selectedImage = myImageView?.image
navigationController?.pushViewController(myCollectionViewController, animated: true)
但是,我对 UICollectionViewCell 进行了子类化,并将单元格设置如下:
import UIKit
class myCollectionViewCell: UICollectionViewCell {
let imageView:UIImageView = {
let iv = UIImageView()
iv.backgroundColor = .red
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
return iv
}()
var selectedImage: UIImage? {
didSet {
self.imageView.image = selectedImage
}
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
如何在 segue 期间直接将我的图像直接传递给我的 UICollectionViewCell 子类?
【问题讨论】:
-
你不能将图像直接传递给segue中的单元格;细胞还不存在。您可以将值传递给视图控制器,它可以在创建单元格时使用它
-
那么,我必须在 myCollectionViewController 中初始化 imageView,而不是在子类单元格中初始化它?
-
你可以从collection view的cellForRowAt方法做到这一点。
标签: ios swift uicollectionview segue uicollectionviewcell