gpt4 book ai didi

ios - 我的代码中出现 NSInternalInconsistencyException 错误

转载 作者:行者123 更新时间:2023-11-28 15:19:10 24 4
gpt4 key购买 nike

以下是我的代码:

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 还是有点陌生​​,这是我遇到的第一个错误,尤其是在我的代码上下文中。

最佳答案

您正在为数据源方法使用较旧的 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)
}

关于ios - 我的代码中出现 NSInternalInconsistencyException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46313131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com