Использование неразрешенного идентификатора «ячейка» в проекте xcode - PullRequest
0 голосов
/ 05 июня 2019

В моем проекте приложения 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.Это позволило бы мне щелкнуть карты одним щелчком.

Ответы [ 2 ]

0 голосов
/ 05 июня 2019

Прежде всего вы неправильно используете 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) { ... }

Ошибка довольно очевидна: вы никогда не объявляли, что cell является областью действия didSelectItemAt. Изменение

let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
0 голосов
/ 05 июня 2019

Я думаю, что вы забыли установить повторно используемый идентификатор

Storyboard set cell identifier

Надеюсь, это решит вашу проблему.

Чтобы получить доступ к этому методу ячейки, вы должны объявить переменную пусть cell = collectionView.cellForItem (at: indexPath) как! CardCollectionViewCell

...