Match game tap bug

I built the match game following the video, but there is a bug that I tap one card, it won’t flip until I tap another one. Does anyone know what happend? And how to solve it?

Can you show us your code where you have the flip functions?

The viewcontroller is as below:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

var model = CardModel()
var cardArray = [Card]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    //call the getCard method of the card model,给上面声明的变量赋值
    cardArray = model.getCards()
    
    collectionView.delegate = self
    collectionView.dataSource = self
    
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    //dispose of any resource that can be recreated.
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    return cardArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    //get a CardCollectionViewCell object
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cardCell", for: indexPath) as! CardCollectionViewCell
    
    //get the card that the collection view is trying to display
    let card = cardArray[indexPath.row]

    //set that card for the cell
    cell.setCard(card)
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
   
    let cell = collectionView.cellForItem(at: indexPath) as! CardCollectionViewCell
    
    let card = cardArray[indexPath.row]
    
    if card.isFlipped == false{
        cell.flip()
        card.isFlipped = true
    }else {
        cell.flipback()
        card.isFlipped = false
    }
  
}

}

Bumping up because I have the same error. Just finished with lesson 9 of the Match App tutorial and this is the only thing that’s troubling me so far.

Hi Ross,

Welcome to the Code Crew community.

Can you post your code from both your ViewController.swift file and your CardCollectionViewCell.swift file. That will enable us to hopefully identify the issue.

Copy and paste the code in a reply. In order to format each code block nicely put 3 back-ticks (ie, 3 of these `) before and after your code on a separate line. The back-tick character is the one below the tilde ~.

ViewController.swift


import UIKit


class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {


    @IBOutlet weak var collectionview: UICollectionView!
    

    let model = CardModel()
    var cardsArray = [Card]()

    
    override func viewDidLoad() {
        super.viewDidLoad()
        cardsArray = model.get_cards()
        collectionview.dataSource = self
        collectionview.delegate = self
    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cardsArray.count
    }

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

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
        if cell?.card?.isFlipped == false {
            cell?.flip_up()
        } else {
            cell?.flip_down()
        }
    }


}

CardCollectionViewCell.swift

import UIKit


class CardCollectionViewCell: UICollectionViewCell {
    
    
    @IBOutlet weak var frontimageview: UIImageView!
    @IBOutlet weak var backimageview: UIImageView!
    
    
    var card:Card?
    
    
    func configure_cell(card:Card) {
        self.card = card
        frontimageview.image = UIImage(named: card.imageName)
        if card.isFlipped {
            flip_up(speed: 0)
        } else {
            flip_down(speed: 0)
        }
    }
    
    
    func flip_up(speed:TimeInterval = 0.3) {
        UIView.transition(from: backimageview, to: frontimageview, duration: speed, options: [.showHideTransitionViews,.transitionFlipFromLeft], completion: nil)
        card?.isFlipped = true
    }

    
    func flip_down(speed:TimeInterval = 0.3) {
        UIView.transition(from: frontimageview, to: backimageview, duration: speed, options: [.showHideTransitionViews,.transitionFlipFromLeft], completion: nil)
        card?.isFlipped = false
    }
    
    
}

Your collectionView method when you tap on the cell should be didSelectItemAt

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
        if cell?.card?.isFlipped == false {
            cell?.flip_up()
        } else {
            cell?.flip_down()
        }
    }

What you have is didDeselectItemAt

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
        if cell?.card?.isFlipped == false {
            cell?.flip_up()
        } else {
            cell?.flip_down()
        }
    }

I have accidentally done this myself so it’s a trap when trying to select the correct method from the AutoComplete options that are presented.

Thank you so much!!!

Works like a charm. Can’t wait to continue the lesson

1 Like

Yaaaaaaaay!!! Great news.