gpt4 book ai didi

ios - 意外发现 nil UIImageView

转载 作者:行者123 更新时间:2023-11-29 10:21:29 25 4
gpt4 key购买 nike

我有一个 UICollectionViewController :

import UIKit

private let reuseIdentifier = "Cell"

class PhotosCollectionViewController: UICollectionViewController {

let photos = [
UIImage(named: "photos_1"),
UIImage(named: "photos_2"),
UIImage(named: "photos_3"),
UIImage(named: "photos_4"),
UIImage(named: "photos_5"),
UIImage(named: "photos_6"),
UIImage(named: "photos_7"),
UIImage(named: "photos_8"),
UIImage(named: "photos_9"),
UIImage(named: "photos_10"),
UIImage(named: "photos_11"),
UIImage(named: "photos_12"),
UIImage(named: "photos_13"),
UIImage(named: "photos_14"),
UIImage(named: "photos_15"),
UIImage(named: "photos_16"),
UIImage(named: "photos_17"),
UIImage(named: "photos_18")
]

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Register cell classes
self.collectionView!.registerClass(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

// MARK: UICollectionViewDataSource

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return photos.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! PhotoCollectionViewCell

cell.imageView.image = self.photos[indexPath.row]
// cell.backgroundColor = UIColor.blueColor()

return cell
}

// MARK: UICollectionViewDelegate

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return false
}

override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
return false
}

override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {

}
*/

}

和单元VC:

import UIKit

class PhotoCollectionViewCell: UICollectionViewCell {

@IBOutlet var imageView: UIImageView!
}

在我的 Assets 中,我将所有图像列在照片数组中 -> http://prntscr.com/9tb7go

在这一行,我得到一个错误:

cell.imageView.image = self.photos[indexPath.row]

错误:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)

最佳答案

我看到你这样做了:

self.collectionView!.registerClass(PhotoCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

在您的 viewDidLoad 中。

如果 Storyboard中有单元格,则应删除该行。该类已在 Interface Builder 中注册,因此该行将替换 IB 创建的实体,并且它不知道您的 imageView 是谁。

如果单元格位于单独的 xib 而不是 Storyboard 中,则必须注册 Nib 而不是类,如下所示:

self.collectionView!.registerNib(UINib(nibName: "name_of_your_nib_without_extension", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)

关于ios - 意外发现 nil UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34945496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com