2017-09-20 7 views
0

Das folgende ist mein Code:NSInternalInconsistencyException Fehler in meinem Code

import UIKit 

class FriendsController: UICollectionViewController { 

    private let cellId = "cellId" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView?.backgroundColor = UIColor.red 

     collectionView?.register(FriendCell.self, forCellWithReuseIdentifier: cellId) 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 
     return 3 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
     return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) 
    } 

    } 

    class FriendCell: UICollectionViewCell { 

    override init(frame: CGRect) { 

     super.init(frame: frame) 
     setUpViews() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    func setUpViews() { 
     backgroundColor = UIColor.blue 
    } 
} 

und folgenden mein Fehler:

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 
<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}' 

Ich bin nicht ganz sicher, was ich falsch gemacht habe? Wenn jemand helfen könnte, sowohl meinen Fehler zu erklären, als auch wie er es beheben könnte, würde es sehr geschätzt werden. Ich bin Swift immer noch ein bisschen neu, und das ist mein erster Fehler, den ich nicht verstehen konnte, besonders im Zusammenhang mit meinem Code.

+0

Sie müssen die Swift-3-Datenquelle Methoden zur Verfügung zu stellen, nicht auf die Swift 2 Methoden. – rmaddy

+0

Was meinst du damit? Wie ich schon sagte, ich bin neu in Swift. –

Antwort

1

Sie verwenden die ältere Swift 2-API für die Datenquellenmethoden.

Statt:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath) 
} 

Verwendung:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) 
} 
+0

Arbeitete! Ich danke dir sehr :) –