ios - 在 xcode 项目中使用未解析的标识符 'cell'

标签 ios swift xcode uicollectionview uicollectionviewcell

在我的比赛应用程序 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 上运行比赛应用程序示例。它允许我点击一下即可翻转卡片。

最佳答案

我认为您忘记设置可重用标识符

Storyboard set cell identifier

我希望这能解决您的问题。

要访问该单元格方法,您必须声明变量 让 cell = collectionView.cellForItem(at: indexPath) as!CardCollectionViewCell

关于ios - 在 xcode 项目中使用未解析的标识符 'cell',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56451586/

相关文章:

swift - 如何在 SwiftUI 中使用来自网络调用的数据填充文本字段?

ios - 了解 Crashlytics 错误日志

javascript - 使用meteor.js 分发cocoa 应用程序

swift - 异步等待 "Type of expression is ambiguous without more context"错误 - 在 swiftUI 中

ios - 我怎样才能继续播放 UILocalNotification 声音直到用户关闭它?

ios - 通知 APNS 关于角标(Badge)计数

ios - 简单 gui 元素的性能 : quartz 2d or png?

iOS:使用终端搜索 cocoapods

iphone - UILocalNotification 的警报 Action 代码

ios - 如何在 Swift 中获取 'expected' 类型的 CoreData 属性?