2017-12-06 2 views
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 
    } 

Dieser Code unten nicht, wie es sollte funktioniert und jedes Mal laufe ich die App es mit diesem Fehler kommt --Sammlung Ansicht endet nach cellForItemAtIndex

 
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) 

Jede mögliche Hilfe bei diesem würde sehr geschätzt werden.

Und der Code, die Fixierung benötigt, ist der folgende Code:

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

Es ist nun Fehler wie diese error und ich bin nicht sicher, wie es es tut? Weil das Bild an ein Array gesendet wird, aber nicht in der UICollectionViewCell angezeigt wird?

+1

bitte nicht, Ihre Beiträge verwüsten. Sobald Sie einen Beitrag eingereicht haben, haben Sie den Inhalt für die gesamte Stack Overflow-Community lizenziert (unter der CC-by-SA-Lizenz). – Mithrandir

Antwort

0

Bitte verwenden Sie die Xcode-Code-Vervollständigung, um sicherzustellen, dass Sie die richtigen Methodensignaturen bereitstellen.

Das Verfahren muss sein:

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

nicht:

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

Hinweis cellForItemAtindexPathcellForItemAt sein sollte und NSIndexPathIndexPath sein sollte.

Damit Ihre ganze wird:

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 
} 

Sehen Sie, wie Sie sollten die Zelltyp-Kraft gegossen. Sie möchten, dass dies früh in der Entwicklung abstürzt, wenn Sie Ihr Zellen-Setup falsch haben.

+0

Danke! Ich habe ein neues Problem damit nicht in der UICollectionView ?? Ich habe Informationen über –

+0

geschrieben. Das ist ein völlig anderes Problem als das, das in Ihrer ursprünglichen Frage gefragt wurde. Sie sollten diese Frage abschließen, indem Sie die Antwort akzeptieren, die Ihre ursprüngliche Frage am besten gelöst hat. Stellen Sie dann bei Bedarf eine neue Frage mit allen für das neue Problem relevanten Details ein. – rmaddy

+0

@ J.David Ich sehe Sie diese Antwort nicht akzeptiert. Wird Ihre gestellte Frage nicht gelöst? – rmaddy

0

Überprüfen Sie Ihren ViewController in main.storyBoard auf jede Art von Warnung, die den normalen Arbeitsablauf beeinträchtigt. Klicken Sie mit der rechten Maustaste auf PhotoCell, das Sie in GalleryController erstellt haben. Wenn eine Warnung angezeigt wird, wird eine Dialogbox angezeigt. Entfernen Sie es, indem Sie auf die Schaltfläche zum Schließen klicken. Oder klicken Sie mit der rechten Maustaste auf das obere linke Icon von GallerViewController, Sie werden ein Popup sehen und jede Art von gelben Warnhinweisen überprüfen. Wenn dies der Fall ist, entfernen Sie sie, indem Sie auf die Schaltfläche Schließen klicken.

zum Beispiel siehe die folgenden Bilder. enter image description here

Verwenden cellForItemAt statt 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! 
    } 
Verwandte Themen