Regarding The Card Game: Implementing the Protocols and Delegates Pattern (2020) - Lesson 23

Hello everyone, I was watching The Card Game: Implementing the Protocols and Delegates Pattern (2020) - Lesson 23
I just can’t understand some things. Could someone help me understand them?

  1. About this line of code “collectionView.dataSource = self” and the like, you said, “Set the view controller as the datasource of the collection view”. Does that mean, the view controller is set as the object of the dataSource, i.e., the view controller is set as the delegate of the dataSource?
  2. Why do we have to write this code “collectionView.dataSource = self” and the like inside override func viewDidLoad() ?
  3. In the duqueueReusableCell method, there’s a parameter “indexPath”. This parameter specifies the location of the item or the cell. However, I don’t see any code that assign a value to that parameter. How can it know what cell or item we are talking about ?
  4. All the code is written inside the class of ViewController, but where is the object or instance of ViewController class ?

This is the way I understand what Chris Ching says:

  • The keyword self refers to the ViewController itself.
    The View Controller that contains the collectionView methods is responsible for two things:
  • A: it provides the data that the collectionView relies on and that is in the form of an array hence the line of code collectionView.datasource = self and
  • B: it responds to user interaction by way of gestures on the collectionView be they taps, swipes, scroll etc hence the line of code collectionView.delegate = self
  • The ViewController is the object that provides the data (dataSource) and is the object that deals with user interaction (delegate).
  • viewDidLoad() is the very first method that runs after the ViewController User Interface is rendered at run time so code in there is run which sets up the ViewController and whatever else has been configured in it such as the collectionView.
  • A Collection View (and a Table View) requires a mechanism to identify the object on the screen that the user has tapped on (in this case the card). It does that via the indexPath which is an internal pointer. The UI is rendered with 16 cards so the top left element is 0 and the bottom right element is 15 remembering that the array index starts at 0. When referring to an item in a collectionView we use the notation indexPath.row where row is an integer and in our case, that integer refers to the position in the array of cards.
  • That’s what you created in the storyboard. The ViewController is configured in storyboard with the required elements and the ViewController.swift file is the code behind it making it do what you want it to do.

Thank you!
I’m sorry, but what does “render” in “ The UI is rendered with 16 cards” mean?

In addition, my thought is that indexPath should not contain those values in the first place. So, I still can’t figure out when and how values are assigned to indexPath ?

Rendered simply means created. It’s a graphical user interface so what you see is “rendered” by the operating system.

Think of an indexPath in a collectionView as being similar to an index position in an array.

let arrayIndex = 4
print(myArray[arrayIndex]) is going to print the 5th element in the array.

In the MatchApp, when you tap on a card, the collectionView delegate recognises that a card has been tapped. It say’s, “OK that was the 5th card in the list so that means that the indexPath.row value is 4”

Thank you!!

Regarding the indexPath
I was wondering whether I can understand indexPath in this way.

The parameter indexPath is automatically assigned values from [0,0] to [0,15] when it’s decided to show 16 cards/cells as determined in this func collectionView( _ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int.

Try not to overthink the process. In time everything makes more sense.

Yes, there are two parts to the IndexPath and they are section and row. When you want to know the row value the notation is indexPath.row and when you want to know the section value the notation is indexPath.section.

For the most part during the early stages of the courses Chris Ching creates, the section property is ignored just to make things simpler to understand.

Thanks for your help!!

A post was split to a new topic: Match App Card game question