tableview:cellForRowAtIndexPath method


#1

I’m not wrapping my head around this method. I don’t understand how it loops through all the rows of the UITableView object. At no point did we loop through the object so I don’t get how it still fetches all the descriptions from the RandomPossession objects. Hopefully someone can explain to me the whole process behind that.


#2

It’s important to remember that it’s really the tableView that’s in control here.

In a departure from what you might expect, we (the authors of the view controller subclass) aren’t the ones looping through the cells.

The tableView asks its delegate/datasource (our view controller) for a number of rows to display. Then the tableView starts a loop with that many iterations, asking us to create and give it back a cell for each row that it thinks it needs to display. The UITableView instance is the driving force behind its own refresh process. Our view controller object merely sits idly by, ready to answer the tableView’s questions about how many cells to draw and what each cell’s contents should be.

We will receive the tableView:cellForRowAtIndexPath: callback once for every cell the tableView needs to display, just before the tableView needs to display it. The callback specifies a specific table row that it wants to display, and it’s our job to hand back an actual cell for the tableView to display at that location. This usually involves peeking at our data model and finding the appropriate/matching object in an array to put on the cell.

Does that help?


#3

Yes that’s excellent! Thank you so much. The process was hazy to me at best, but I understand now that everything emanates from UITableView, not it’s controller.