swift - Collection View 在 cellForItemAtIndex 之后终止

标签 swift imageview cell collectionview indexpath

import UIKit
import Photos

class GalleryController: UICollectionViewController,
                         UIImagePickerControllerDelegate,
                         UINavigationControllerDelegate {
    var Receipts = [UIImage?]()

    //Number of Views
    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        print("return1")
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        print("self.receipts.count")
        return self.Receipts.count
    }

下面的代码无法正常工作,每次运行应用程序时都会出现此错误 -

SmartReceipts[738:137366] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the collection view's data source did not return a valid cell from -collectionView:cellForItemAtIndexPath: for index path  {length = 2, path = 0 - 0}'
*** First throw call stack:
(0x186196364 0x1853dc528 0x186196238 0x186b317f4 0x1901010b0 0x18f6d7124 0x18f6d1de0 0x18f674f00 0x18a1d9998 0x18a1ddb20 0x18a14a36c 0x18a171b90 0x18f66a5c8 0x18613dedc 0x18613b894 0x18613be50 0x18605be58 0x187f08f84 0x18f6db67c 0x104484668 0x185b7856c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Any help with this would be greatly appreciated.

And the code that needs fixing is the code below:

    func collectionView(_ collectionView: UICollectionView, cellForItemAtindexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "receipt", for: indexPath as IndexPath) as? PhotoCell

        cell?.imageView.image = self.Receipts[indexPath.row]
        print("Assigned to cell and should come up")
        return cell!
    }
}

现在错误如下 error我不确定它是如何做到的?因为它将图像发送到数组但它没有显示在 UICollectionViewCell 中?

最佳答案

请使用 Xcode 的代码完成功能来确保您提供正确的方法签名。

该方法需要是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

不是:

func collectionView(_ collectionView: UICollectionView, cellForItemAtindexPath indexPath: NSIndexPath) -> UICollectionViewCell

注意cellForItemAtindexPath应该是cellForItemAt并且NSIndexPath应该是IndexPath

这样,你的整体就变成了:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "receipt", for: indexPath) as! PhotoCell

    cell.imageView.image = self.Receipts[indexPath.row]
    print("Assigned to cell and should come up")
    return cell
}

了解应如何强制转换细胞类型。如果您的单元设置不正确,您希望它在开发早期崩溃。

关于swift - Collection View 在 cellForItemAtIndex 之后终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47683813/

相关文章:

ios - UITextbox 和 Navigationcontroller

ios - 与当前时间的差异 - 使用 String 而不是 dateWithEra

java - 控制java中的循环执行速度 - Android

arrays - Matlab:将双向量数组转换为字符串元胞数组

swift - 查找 swift 框架中的新增功能

swift - 如何递增十六进制数

Android 布局和自动调整图像大小

android - 从 Firebase 存储中检索图像 url 并将其放入警报对话框中

excel - 使用 VBA 为 Excel 中的一系列单元格着色

html - 如何使 HTML 表格单元格可编辑?