UICollectionViewControllerにセグメンテーションするたびに、ビューコントローラからUICollectionViewCellに画像を渡そうとしています。通常は、変数をUICollectionViewControllerに直接渡すために次のコードを使用します。変数を別のビューコントローラから直接UICollectionViewCellに渡します。
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")
}
}
は、どのように私は直接セグエ中に私のUICollectionViewCellサブクラスに直接自分のイメージを渡すのですか?
あなたはセグエのセルに画像を直接渡すことはできません。細胞はまだ存在しません。ビューコントローラに値を渡すことができ、セルを作成するときに使用できます – Paulw11
したがって、サブクラス化されたセル内で初期化するのではなく、myCollectionViewControllerでimageViewを初期化する必要がありますか? – Tom
これはコレクションビューのcellForRowAtメソッドから行うことができます。 –