集合视图在cellForItemAtIndex

时间:2017-12-06 21:35:06

标签: 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) 

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

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

    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中?

2 个答案:

答案 0 :(得分:0)

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

方法必须是:

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

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

注意cellForItemAtindexPath应为cellForItemAtNSIndexPath应为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
}

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

答案 1 :(得分:0)

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

例如,请参阅下面的图片。enter image description here

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