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
    }

This code below is not working as it should and everytime I run the app it comes up with this error -

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

It now errors as this error and i'm not sure how it's doing it? Because it is sending the image to an array but it's not showing up in the UICollectionViewCell?

tripleee
  • 175,061
  • 34
  • 275
  • 318
J.David
  • 1
  • 4
  • 1
    Please do not vandalise your posts. Once you have submitted a post, you have licensed the content to the Stack Overflow community at large (under the CC-by-SA license). – Mithical Dec 07 '17 at 13:51

2 Answers2

0

Please use Xcode's code completion to ensure you are providing the correct method signatures.

The method needs to be:

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

not:

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

Note cellForItemAtindexPath should be cellForItemAt and NSIndexPath should be IndexPath.

With that, your whole becomes:

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
}

See how you should force-cast the cell type. You want this to crash early on in development if your have your cell setup incorrectly.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks! I have a new problem with it not appearing in the UICollectionView?? I have posted information above – J.David Dec 06 '17 at 23:13
  • That's a completely different issue from the one asked in your original question. You should close off this question by accepting the answer that best solved your original question. Then, if needed, post a new question with all relevant details specific to the new issue. – rmaddy Dec 06 '17 at 23:16
  • @J.David I see you unaccepted this answer. Does it not solve your posted question? – rmaddy Dec 08 '17 at 23:33
0

Check your ViewController in main.storyBoard that any kind of warning that affect normal flow of working. right click on PhotoCell you have created in GalleryController, a dialogbox will appear, if there is any kind of warning. remove it by clicking close button. or else right click on top left icon of GallerViewController,you will get a popup and check any kind of yellow Warning is there.if so then remove it by clicking close button.

for example see the below images.enter image description here

Use cellForItemAt instead of 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!
    }
Junaid Ali
  • 175
  • 1
  • 2
  • 14