【问题标题】:Populating UICollectionView with images is returning nil : Swift 2用图像填充 UICollectionView 返回 nil:Swift 2
【发布时间】:2024-01-05 01:12:01
【问题描述】:

我有一个UICollectionView,其中有一个带有UIImageView 的自定义单元格,还有一个带有插座的自定义类

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   
}

【问题讨论】:

  • 我不知道你说的返回 nil 是什么意思?
  • @AndriusSteponavičius 致命错误:在展开可选值时意外发现 nil
  • 如果是png就不需要放.png
  • 你设置 yourcollection.delegate = self 和 yourcollection.dataSource = self 了吗?
  • @Totka 我添加了这些,但仍然有同样的错误

标签: ios iphone swift uicollectionview swift2


【解决方案1】:

如果你已经添加了这一行:

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
    }
}

CollectionViewCell.swift

import UIKit

class CollectionViewCell: UICollectionViewCell {


    @IBOutlet var theImage:UIImageView!

}

别忘了将标识符分配给您的单元格:

Sample 了解更多信息。

【讨论】:

    【解决方案2】:

    尝试先在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   
    }
    

    【讨论】:

    • 对于 nibName 我可以使用任何东西或 UITableViewCell 类名吗?
    • 你必须使用UICollectionViewCell类名。
    【解决方案3】:

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

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

    【讨论】: