こんにちは!問題を正確に示すサンプルプロジェクトを作成しました。クリックしてダウンロード!
のサブクラスを含むアプリを作成しました。UICollectionViewCell
これにはImageView
asサブビューが含まれています。また、ビューを含むXIBファイルもあります。
に直接埋め込まれているUICollectionViewCell
内でこれを使用UICollectionView
するViewController
と、セルは正常に機能します。セルを登録するコードを含むWorkingViewControllerのコードは次のとおりです。
class WorkingViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
let cellNib = UINib(nibName: "ImageCell", bundle: nil)
collectionView.register(cellNib, forCellWithReuseIdentifier: "ImageCell")
collectionView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func randomInt(min: Int, max: Int) -> Int {
return Int(arc4random_uniform(UInt32(max - min + 1))) + min
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as? ImageCell else {
fatalError("Could not load cell")
}
let randomNumber = randomInt(min: 1, max: 10)
cell.imageView.image = UIImage(named: "stockphoto\(randomNumber)")
return cell
}
}
次に、ストーリーボードのエントリポイントをBuggyViewControllerに移動します。ここでは、UICollectionView
は直接埋め込まれていません。が含まれUIView
ていXIB
ます。これがそのビューのコードで、以前のセルの登録が含まれています。
class DetailView: UIView, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var contentView: UIView!
@IBOutlet weak var collectionView: UICollectionView!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
Bundle.main.loadNibNamed("DetailView", owner: self, options: nil)
self.addSubview(self.contentView)
self.contentView.frame = self.bounds
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
collectionView.delegate = self
collectionView.dataSource = self
let cellNib = UINib(nibName: "ImageCell", bundle: nil)
collectionView.register(cellNib, forCellWithReuseIdentifier: "ImageCell")
collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell")
collectionView.reloadData()
}
func randomInt(min: Int, max: Int) -> Int {
return Int(arc4random_uniform(UInt32(max - min + 1))) + min
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as? ImageCell else {
fatalError("Could not load cell")
}
let randomNumber = randomInt(min: 1, max: 10)
cell.imageView.image = UIImage(named: "stockphoto\(randomNumber)")
return cell
}
}
ただし、これにより、DetailViewの55行目にエラーが発生します。
スレッド1:致命的なエラー:オプション値のアンラップ中に予期せずnilが見つかりました
したがって、ここimageView
ではセルのが突然nilになり、セルの登録またはインスタンス化で何か間違ったことをしたと思います。しかし、私はそれを解決することはできません。
セルクラスを登録する行を削除する必要があります。
collectionView.register(ImageCell.self, forCellWithReuseIdentifier: "ImageCell")
実際、セルをペン先で定義したので、メソッドを使用してセルを登録する必要がありますfunc register(_ nib: UINib?, forCellWithReuseIdentifier identifier: String)
。cellClassメソッドを介して登録すると、nilが返されます。
そして、Appleのドキュメントが述べているように:
以前に同じ再利用識別子でクラスまたはnibファイルを登録した場合は、cellClassパラメーターで指定したクラスが古いエントリを置き換えます。指定された再利用識別子からクラスの登録を解除する場合は、cellClassにnilを指定できます。
これはあなたがゼロになった理由を説明しています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加