【发布时间】:2020-07-05 17:51:05
【问题描述】:
我正在关注 Duc Tran 的集合教程 (https://www.youtube.com/watch?v=vB-HKnhOgl8),当我运行我的应用程序时,我不断收到此错误:
线程 1:异常:“无法使类型视图出列:带有标识符 CHALLENGECELL 的 UICollectionElementKindCell - 必须为标识符注册 nib 或类或连接情节提要中的原型单元”
我为此查看了其他解决方案,但没有一个产生结果。
这是保存集合的视图控制器的代码
import UIKit
class ChallengesViewerViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
//MARK: - UICollectionViewDataSource
private var challenges = WeeklyChallenges.createChallenge()
let cellScaling: CGFloat = 1.0
override func viewDidLoad() {
super.viewDidLoad()
let screenSize = UIScreen.main.bounds
let cellWidth = floor(screenSize.width * cellScaling)
let cellHeight = floor(screenSize.height * cellScaling)
let insetX = (view.bounds.width - cellWidth)/2
let insetY = (view.bounds.height - cellHeight)/2
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: cellWidth, height: cellHeight)
collectionView.contentInset = UIEdgeInsets(top: insetY, left: insetX, bottom: insetY, right: insetX)
collectionView.dataSource = self
}
}
extension ChallengesViewerViewController: UICollectionViewDataSource{
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return challenges.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CHALLENGECELL", for: indexPath) as! ChallengeCollectionViewCell
cell.challenge = self.challenges[indexPath.item]
return cell
}
}
此外,here 是我将可重用标识符分配给集合视图单元格的情节提要。
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell