Wiring up the table view ... Question?


#1

page 187 there’s this bit of code… (I’ve left out the comments)

- (UITableViewCell *)tableView:(UITableView *)tableView
   cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *c = [taskTable dequeueReusableCellWithIdentifier:@"Cell"];

if (!c) {
c = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:@"Cell"];
}

NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];

return c;


}

Ok, so I get that the first bit of code is reusing cells as they scroll of the screen, but what I’m trying to grasp about the last bit is how the tableView is pulling the proper tasks from the NSMutableArray. I guess more specifically, the lines:

NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];

I get that first line is grabbing the string for a todo item in the tasks array, and the second line provides that to the cell in the table view. But how does it know to do that for each of the values in the array? On page 188 we hard code a handful of todo items into the tasks array, but does the (UITableViewCell *) ... know to go through each item in the array? Or does NSString *item = [tasks objectAtIndex:[indexPath row]]; automatically loop through the array until it’s reached the end?

I hope I’m making sense in the way I asked this.


#2

My understanding is that the method tableView:cellForRowAtIndexPath: is called once for each row visible in the table view each time that view is drawn.