【问题标题】:Use of unresolved identifier 'cell' in xcode project在 xcode 项目中使用未解析的标识符“单元格”
【发布时间】:2019-06-04 21:28:14
【问题描述】:

在我的比赛应用 Xcode 项目中,我有一个错误说明:

使用未解析的标识符“单元格”。

我的代码:

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 上运行匹配应用程序示例。它可以让我一键翻转卡片。

【问题讨论】:

    标签: ios swift xcode uicollectionview uicollectionviewcell


    【解决方案1】:

    我想你忘了设置可重用标识符

    希望这能解决您的问题。

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

    【讨论】:

      【解决方案2】:

      首先你误用了collectionView:willDisplay:forItemAt。将所有内容移至collectionView:cellForItemAt,将return 替换为return cell 并删除willDisplay:

      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell =  collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! CardCollectionViewCell
      
          //get card that
          let card = cardArray[indexPath.row]
          cell.setCard(card)
          return cell
      }
      

      罢工>

         func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { ... }
      

      错误很明显:您从未声明 celldidSelectItemAt 的范围。改变

      let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
      

      【讨论】:

      • 我知道答案的日期。
      • 当然要删除类开头的方法cellForItemAt。请不要成为复印机和粘贴机。学习这些东西。
      • 请提出一个新问题,如果有帮助,请接受这个问题。
      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多