意外地发现了无UIImageView

时间:2016-01-22 11:19:28

标签: ios uiimageview uicollectionview

我有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!
}

在我的资产中,我已将所有图像列为照片阵列 - > http://prntscr.com/9tb7go

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

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

错误:

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

1 个答案:

答案 0 :(得分:2)

我看到你这样做了:

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

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

如果您将单元格放在单独的xib而不是故事板中,则必须注册nib而不是类,如下所示:

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