No. I think you get confused where I am starting only now to understand how its all work… I will try to explain what I understand…
.
.
.
- We declare a variable:
var card = Card?
variable “card” of type “Card” as optional (?).
Card is the class created in “Card.swift”
class Card {
var imageName: String = ""
var isMatch: Bool = false
var isFlipped: Bool = false
}
therefore, we have:
card.imageName = ""
card.isMatch = false
card.isFlipped = false
(until now, we were talking about the variable “card” that I have renamed for your understanding “theMainCard” above)
.
.
.
- In the method “collectionView” delegate Methods (willDisplay cell forItemAt indexPath), we are calling the function “configureCell()” with the value “cardsArray[indexPath.row]” as “card:” parameter
let cardCell = cell as? CardCollectionViewCell
cardCell?.configureCell(card: cardsArray[indexPath.row])
“indexPath.row” is a kind of Array of Integer which denominate a cell [Int] = [0, 1, 2, …]
.
.
.
3) We have to keep in mind that “cardArray” is a Dictionary containing 16 times a “Card”
var cardsArray = [Card]()
cardsArray = model.getCards()
So “cardArray” should look like something like that:
var cardArray = [
card[0] -> [ imageName = "card6",
isMatch = false,
isFlipped = false
],
card[1] -> [ imageName = "card12",
isMatch = false,
isFlipped = false
],
card[2] -> [ imageName = "card6",
isMatch = false,
isFlipped = false
],
(...)
card[15] -> [ imageName = "card4",
isMatch = false,
isFlipped = false
]
]
and the “indexPath.row” will loop requesting one of the index of “cardArray” (i.e. cardArray[0])
.
.
.
- When the the function “configureCell()” with the parameter “card” is called, we are “copying” one of the “Card” from "cardArray’ in the above variable “card” (that we have declared in point 1 as an optional empty Card - and I called in the previous thread “theMainCard”).
We have to use the prefix “self.” as it has been set outside of the function.
func configureCell(card:Card) {
self.card = card
}
Now, our variable “card” from point 1 is equal to our first “Card” from “cardsArray” (cardArray[0] if indexPath.row = 0 in my example).
card.imageName = "card6",
card.isMatch = false,
card.isFlipped = false
.
.
.
5.) We then check
if card.isMatch == true {
return
}
but as “card.isMatch” is false, it exit the function “configureCell()” (with “return”) and the second cell (IndexPath.row = 1), then the third… until they get recycled!
I hope this will help you to understand how it works and that I didn’t make too many terminology mistakes - I am a newbie too!
Maybe Chris (or someone from his team) can correct where I am wrong if so.
Now that we have demonstrate that the value is used, could you maybe share your project so we can have a lot why it is acting weird?