Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indexPath.row is always 0

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.

like image 347
Simon Lee Avatar asked Nov 20 '11 06:11

Simon Lee


2 Answers

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.

like image 161
DavidA Avatar answered Oct 06 '22 01:10

DavidA


I have just had a similar issue (indexPath.row was always 0) and noticed how much of an idiot I was. In my numberOfSectionsInTableView I was returning the [dataSet count] and in my tableView:numberOfRowsInSection I was returning 1. Should be the other way around. Oops!

like image 44
micnguyen Avatar answered Oct 06 '22 00:10

micnguyen