Ios 用图像填充UICollectionView将返回nil:Swift 2

Ios 用图像填充UICollectionView将返回nil:Swift 2,ios,iphone,swift,uicollectionview,swift2,Ios,Iphone,Swift,Uicollectionview,Swift2,我有一个UICollectionView,其中有一个自定义单元格,其中有一个UIImageView,还有一个自定义类,其中有一个outlet class theCell: UICollectionViewCell { @IBOutlet var theImage:UIImageView! } 我有一个UIImage数组(它只有一个常量图像)。图像位于主捆绑包中 var tempImages = [UIImage(named: "placeholder.png" )] 委托方法 overri

我有一个
UICollectionView
,其中有一个自定义单元格,其中有一个
UIImageView
,还有一个自定义类,其中有一个outlet

class theCell: UICollectionViewCell { 
@IBOutlet var theImage:UIImageView!
}
我有一个
UIImage
数组(它只有一个常量图像)。图像位于主捆绑包中

var tempImages = [UIImage(named: "placeholder.png" )]
委托方法

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return tempImages.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = self.collectionView?.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! theCell

    //Unexpectadly found nil
    cell.theImage.image = tempImages[indexPath.row]   
    return cell   
}

尝试在
cellForItemAtIndexPath中首先注册:

collectionView.registerNib(UINib(nibName: "FeedCell", bundle: nil), forCellWithReuseIdentifier: "cell")
编辑

在你的情况下:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    collectionView.registerNib(UINib(nibName: "FeedCell", bundle: nil), forCellWithReuseIdentifier: "cell")
    let cell = self.collectionView?.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! theCell
    cell.theImage.image = tempImages[indexPath.row]   
    return cell   
}

如果已添加此行:

self.collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "cell")
然后首先删除此项,下面是完整的工作代码:

CollectionViewController.swift

import UIKit

class CollectionViewController: UICollectionViewController {

    var tempImages = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tempImages.append(UIImage(named: "bg.jpg")!)
    }

    // MARK: UICollectionViewDataSource

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


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

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

        // Configure the cell
        cell.theImage.image = tempImages[indexPath.row]
        println(cell.theImage.image)
        return cell
    }
}
import UIKit

class CollectionViewCell: UICollectionViewCell {


    @IBOutlet var theImage:UIImageView!

}
CollectionViewCell.swift

import UIKit

class CollectionViewController: UICollectionViewController {

    var tempImages = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()
        tempImages.append(UIImage(named: "bg.jpg")!)
    }

    // MARK: UICollectionViewDataSource

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


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

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

        // Configure the cell
        cell.theImage.image = tempImages[indexPath.row]
        println(cell.theImage.image)
        return cell
    }
}
import UIKit

class CollectionViewCell: UICollectionViewCell {


    @IBOutlet var theImage:UIImageView!

}
不要忘记为您的手机分配标识符:


有关详细信息。

问题是重新初始化
tempImages
数组,使其返回
nil

var tempImages = [UIImage(named: "placeholder.png" )]

我不知道你所说的返回为零是什么意思?@AndriusSteponavičius致命错误:如果是不需要放置的png,则在展开可选值时意外发现为零。pngDidi你是否设置了你的collection.delegate=self和你的collection.dataSource=self?@Totka我添加了这些,但对于nibName它仍然存在相同的错误我可以使用任何东西吗或者UITableViewCell类名?必须使用
UICollectionViewCell
类名。