Module 3 - Lesson 10

Hello All!

I’m on Lesson 10 of Module 3 and using Xcode 11.4(11E146)… The issue, the bug of where if you click a card from the 1st row then scroll to the last row to click a card and they don’t match… the 1st card still does NOT flip over even with me using the fix shown in the video…

excerpt from code in ViewController.swift file:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 
    //  Get a cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CardCell", for: indexPath) as! CardCollectionViewCell
    
    //  Method 1 to get the card from the card array
    cell.configureCell(card: cardsArray[indexPath.row])

    //  *** Optional *** Method 2 to get the card from the card array (Simplier example)
    //  let card = cardsArray[indexPath.row]
    //  cell.configureCell(card: card)
    
    //  Return it
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
    //  Get a reference to the cell that was tapped
    let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
    
    //  Check the status of the card to determine how to flip it
    if cell?.card?.isFlipped == false && cell?.card?.isMatched == false{
        cell?.flipUp()
        
        //  Check if this is the first card that was flipped or the second card
        if firstFlippedCardIndex == nil {
            
            //  This is the first card flipped over
            firstFlippedCardIndex = indexPath
        }
        else {
            //  Second card that is flipped - Run the comparison logic
            checkForMatch(indexPath)
        }
    }
}

I found the answer from someone else’s answer… (I’ve marked it in ** BOLD TYPE **)

//  MARK:  - Game Logic Methods

func checkForMatch(_ secondFlippedCardIndex:IndexPath) {
    
    //  Get the two card objects for the two indices and see if they match
    let cardOne = cardsArray[firstFlippedCardIndex!.row]
    let cardTwo = cardsArray[secondFlippedCardIndex.row]
    
    //  Get the two collection view cells that represent card 1 & 2
    let cardOneCell = collectionView.cellForItem(at: firstFlippedCardIndex!) as? CardCollectionViewCell
    let cardTwoCell = collectionView.cellForItem(at: secondFlippedCardIndex) as? CardCollectionViewCell

    //  Compare the two cards
    if cardOne.imageName == cardTwo.imageName {
        
        //  It's a match - Set the status and remove them
        cardOne.isMatched = true
        cardTwo.isMatched = true
        
        cardOneCell?.remove()
        cardTwoCell?.remove()
    }
    else {
        //  It's NOT a match - Flip both cards over
        cardOneCell?.flipDown()
        cardTwoCell?.flipDown()
        **cardOne.isFlipped = false**
        **cardTwo.isFlipped = false**
    }        
    //  Reset the firstFlippedCardIndex property
    firstFlippedCardIndex = nil   
}    

}

Thanks for sharing the solution! I got the same problem.