gpt4 book ai didi

swift - Collection View 在 cellForItemAtIndex 之后终止

转载 作者:行者123 更新时间:2023-11-30 11:57:09 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com