2017-09-20 5 views
0

次は私のコードです:NSInternalInconsistencyExceptionエラー

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

そして、次は私のエラーです:

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

は私が間違っていたものにと正確にはわかりませんか?誰かが私のエラーとそれを修正する方法の両方を説明するのを助けることができれば、それは非常に高く評価されるだろう。私はまだSwiftにとっては少し新しく、これは私の最初のエラーです。特に私のコードの文脈では理解できませんでした。

+0

あなたはスウィフト3データソースの方法ではなく、スウィフト2つの方法を提供する必要があります。 – rmaddy

+0

それはどういう意味ですか?私が言ったように、私はスウィフトの新人です。 –

答えて

1

データソースメソッドに古いSwift 2 APIを使用しています。

の代わりに:

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

用途:

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

Worked!どうもありがとうございます :) –

関連する問題