在我的比赛应用程序 Xcode 项目中,出现错误:
Use of unresolved identifier 'cell'.
我的代码:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
}
@IBOutlet weak var collectionView: UICollectionView!
var model = CardModel()
var cardArray:[Card] = []
override func viewDidLoad() {
super.viewDidLoad()
//call the getCards method of the card model
cardArray = model.getCards()
collectionView.delegate = self
collectionView.dataSource = self
}
//mark: -UICollectionView Protocol Methods
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return cardArray.count
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
//get a cardCollectionViewcell object
let cell =
collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! CardCollectionViewCell
//get card that
let card = cardArray[indexPath.row]
cell.setCard(card)
return
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
_ = collectionView.cellForItem(at: indexPath) as!CardCollectionViewCell
//flip the card
cell.flip() <--------- THIS IS THE ERROR
错误修复后,我应该能够在假 iPhone 上运行比赛应用程序示例。它允许我点击一下即可翻转卡片。
最佳答案
我认为您忘记设置可重用标识符
我希望这能解决您的问题。
要访问该单元格方法,您必须声明变量 让 cell = collectionView.cellForItem(at: indexPath) as!CardCollectionViewCell
关于ios - 在 xcode 项目中使用未解析的标识符 'cell',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56451586/