indexPath.row is always 0

Simon Lee picture Simon Lee · Nov 20, 2011 · Viewed 10.8k times · Source

I'm working on populating the table view with an array of dictionaries. The contents of the arrays and the dictionaries are parsed without problems. However, the table is populated with [array count] number of cells with contents with index 0 of the array. It seems to me that indexPath.row returns 0 for all cells. How do I populate each cell with the corresponding index?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


// Configure the cell...
NSDictionary *term = [self.terms objectAtIndex:indexPath.row];
cell.textLabel.text = [term objectForKey:@"content"];
cell.detailTextLabel.text = [term objectForKey:@"created"];

return cell;
}

Thanks a lot!

Edit: Here's what I got for logging indexPath

2011-11-21 03:07:58.025 Demo[21269:f803] 2 indexes [0, 0]

2011-11-21 03:07:58.027 Demo[21269:f803] 2 indexes [1, 0]

Seems like the first index is updating while the second is not.

When I logged indexPath.row, however

2011-11-21 03:19:40.306 Demo[21546:f803] 0

2011-11-21 03:19:40.308 Demo[21546:f803] 0

So what should I use to get the value that is incrementing?

Thanks for the help again.

Answer

DavidA picture DavidA · Nov 20, 2011

That block of code looks fine in itself. Maybe your tableview has multiple sections with one row each? What are you returning for the datasource methods tableView:numberOfRowsInSection: (should be [self.terms count] ) and numberOfSectionsInTableView: (should be 1).

If those are OK, put NSLog(@"%@",indexPath); somewhere in the method and post the output into your question.