0
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] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“集合视图的数据源未从 -collectionView:cellForItemAtIndexPath 返回有效单元格:索引路径 {length = 2, path = 0 - 0}'
*** 首先抛出调用堆栈:
(0x186196364 0x1853dc528 0x186196238 0x186b317f4 0x1901010b0 0x18f6d7124 0x18f6d1de0 0x18f674f00 0x18a1d9998 0x18a1ddb20 0x18a14a36c 0x18a171b90 0x18f66a5c8 0x18613dedc 0x18613b894 0x18613be​​50 0x18605be58 0x187f08f84 0x18f6db67c 0x104484668 0x185b7856c)
libc++abi.dylib:以 NSException 类型的未捕获异常终止
(lldb)

对此的任何帮助将不胜感激。

需要修复的代码是下面的代码:

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

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

4

2 回答 2

0

检查 main.storyBoard 中的 ViewController 是否有任何影响正常工作流程的警告。右键单击您在 GalleryController 中创建的 PhotoCell,如果有任何警告,将出现一个对话框。通过单击关闭按钮将其删除。或者右键单击 GallerViewController 的左上角图标,您将弹出一个窗口并检查是否存在任何黄色警告。如果是,请单击关闭按钮将其删除。

例如看下面的图片。在此处输入图像描述

使用 cellForItemAt 而不是 cellForItemAtindexPath

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> 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!
    }
于 2017-12-06T22:03:12.953 回答
0

请使用 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
}

看看你应该如何强制转换单元格类型。如果您的单元设置不正确,您希望它在开发早期崩溃。

于 2017-12-06T21:43:21.607 回答