Matchgame cellForItemAt can't remember which card flipped

I’m trying out matchgame tutorial. After getting to lesson 10, I realize that if I select card 0, and scroll all the way down, card 12 will be flipped as well. Even befoer I select it. Some how I feel that cellForItemAt is not remembering the right card that I flip. Or UIView.transition is not recording the information. Any advice on this?

Hi Wee Hoo,

In the tutorial series on the Match App you will find that a little further along in the series that issue is addressed and code is added to prevent that from occurring.

You mean the YouTube version or paid version? I’m using YouTube version and almost to the end of lesson 11 but yet to see anything that might help

it could be the paid version that implements the fix a little differently. Can you share your code and I’ll take a look.

Here is my code. Thanks. BTW, can you provide me details on the paid version? The lesson is so good, I don’t mind paying.

https://drive.google.com/file/d/1qXCfV0p_kOPsJrlJ6GF5QdiC2fnRsmn-/view?usp=sharing

For the paid course details, the best thing to do is take a look at the options here.

In the meantime, I will take a look at your code.

OK so which Lesson are you up to in the YouTube series? I’m assuming you’ve gotten most of the way through.

In Lesson 8 at timestamp 20.11 Chris discussed how the collectionView reuses cells and the potential for a re-used cell to have attributes set that belonged to a cell that is now out of view but now apply to a new cell that has come into view.

At 21:50 he describes the additional code that it required in the setCard() function to deal with that and make sure that cells that are re-used have their attributes set correctly.

The setCard() function will now look like this and the cells that are re-used are set correctly.

    func setCard(_ card: Card) {
        // keep track of the cards that get passed in
        self.card = card
        
        frontImageView.image = UIImage(named:card.imageName)
        
        //  Determine if the card is in a flipped up or flipped down state
        if card.isFlipped == true {
            //  Make sure that the fontImageView is on top
            UIView.transition(from: backImageView, to: frontImageView, duration: 0, options: [.transitionFlipFromLeft, .showHideTransitionViews], completion: nil)
        } else {
            //  Make sure that the backImageView is on top
            UIView.transition(from: frontImageView, to: backImageView, duration: 0, options: [.showHideTransitionViews, .transitionFlipFromLeft], completion: nil)
        }
    }

Also Lesson 9 (https://www.youtube.com/watch?v=nAJ7sDF39II) at 22:45 talks about cards that have been matched being reused so that needs to be taken care of as well otherwise reused cells that have been matched could be visible when they should not be.

A couple of points to bear in mind with your code.
You should add a space between equal signs like this:
let seconds = String(format: "%.2f", milliseconds/1000)

mathematical operators like this:
milliseconds -= 1

opening braces after a function declaration like this:
func checkForMatches(_ secondFlippedCardIndex: IndexPath) {

test statements like this:
if cardOne.imageName == cardTwo.imageName {

All of that is in the interests of making your code comply with general coding conventions, particularly readability, that apply to iOS Development. That will be the case if you are looking for work in an iOS Developer capacity. You will be expected to adhere to naming conventions, code layout conventions, code commenting, structure and formatting.

Thanks. Don’t know why I missed the two important parts even after going through so many times. I’ll also fix the naming convention and also look at the paid version