Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Blog Reader iPhone App Getting Data from the Web What is NSDictionary?

ObjectAtIndex:indexPath.row Confusion

I'm not sure if I missed it somewhere, but I'm having trouble understanding this one line: cell.textLabel.text = [self.titles objectAtIndex:indexPath.row]; How does the indexPath.row work to apply each title to a row? Specifically, what is row doing?

4 Answers

Stone Preston
Stone Preston
42,016 Points

NSIndexPath has 2 properties, row and section. Row tells you the row number, and section tells you the section number. (although a lot of the time you only use row if you only have 1 section in your tableView, which is the case for many tableViews)

the tableView delegate method includes indexPath as a parameter of the method. you use that parameter here:

cell.textLabel.text = [self.titles objectAtIndex:indexPath.row]

so the tableView uses the row of the table to index your array, giving each cell a title based on the data in array.

the first row's indexPath.row is 0, so it uses the first element in the array as the title, the second row's indexPath.row is 1, so it uses the 2nd element in the array as the title, the third row's indexPath.row is 2, so it uses the 3rd element in the array as the title etc etc.

Ok I think I got it. So it's getting the value for its row number from the previous method, and then counting through them while applying the titles?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [self.titles count]; }

Stone Preston
Stone Preston
42,016 Points

eh sort of. That numberOfRows method lets the tableView know how many rows there need to be, but I wouldnt say that it gets its indexPath.row number from that numberOfRows method since that numberOfRows method just returns then length of the array.

Hmm, I guess I'm just still stuck on how each title gets assigned to a cell and where that happens in the table view controller. In Java I was used to running a for loop to work through arrays and assign data.

Stone Preston
Stone Preston
42,016 Points

well the cellForRowAtIndexPath tableView delegate method is what does that. It gets called for each row in the table. and inside that method you have code for specifying what goes in the cell, and it uses the indexPath.row to index your array to get data.

Ah I see now. Thanks!