One quesstion


#1

Hi guys,
There is one problem confused me when I reading the IOS programming, I think I should ask some help for solving the issue.

Code is this, in the chapter 8, I saw the method is

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    NSArray *items  = [[BNRItemStore shareStore] allItems];
   BNRItem *item = items[indexPath.row];
   cell.textLabel.text = [item description];

    return  cell;
}

My question is , I didn’t define indexPath.row , why I can use the variable directly? Excuse for my foolish question. Thanks for kindly helping.

PS. English isn’t my native language, so if I makes some mistake, forgive me please :stuck_out_tongue:


#2

Relax, your English is good!

[quote]My question is , I didn’t define indexPath.row , why I can use the variable directly?..
[/quote]
But you defined indexPath as a parameter of the tableView:cellForRowAtIndexPath: method.

- (UITableView *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ...
}

All parameters are considered local variables in the body of a method; that is why you are able to use indexPath.row.

Table view supplies an IndexPath object as an argument when it invokes the tableView:cellForRowAtIndexPath: method.


#3

[quote=“ibex10”]
All parameters are considered local variables in the body of a method; that is why you are able to use indexPath.row.

Table view supplies an IndexPath object as an argument when it invokes the tableView:cellForRowAtIndexPath: method.[/quote]

Thanks a lot ibex10, actually, I try to print the value of indexPath.row, so I wrote NSLog IndexPath.row. bla bla . The output is 0 1 2 3 4, so do you mean the indexPath object point the items length , NSArray *items = [[BNRItemStore shareStore] allItems]; hence I can use the value of indexPath.row directly.

Thanks again for you kindly reply !


#4

That’s correct: indexPath.row >= 0 && indexPath.row < Number of rows in relevant section.


#5

Thanks very much ibex10. :smiley: