簡體   English   中英

在xcode項目中使用未解析的標識符“cell”

[英]Use of unresolved identifier 'cell' in xcode project

在我的匹配應用程序Xcode項目中,我有一個錯誤說明:

使用未解析的標識符“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上運行匹配應用程序示例。 它可以讓我在點擊時翻轉卡片。

我想你忘了設置可重用的標識符

故事板設置單元格標識符

我希望這能解決你的問題。

要訪問該單元格方法,您必須將變量let cell = collectionView.cellForItem(at:indexPath)聲明為!CardCollectionViewCell

首先,你是在濫用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM