12

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.

beryllium
  • 29,669
  • 15
  • 106
  • 125
Simon Lee
  • 170
  • 1
  • 1
  • 8

2 Answers2

38

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.

DavidA
  • 3,112
  • 24
  • 35
  • Here's what I have 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] I have only one section to my table and I'm returning the right values for `tableView:numberOfRowsInSection:` and `numberOfSectionsInTableView:` – Simon Lee Nov 21 '11 at 09:22
  • 3
    @SimonLee - the logging you are seeing says the table view is asking for section 0, row 0, then section 1, row 0. So I don't think you do have the right implementation for your rows and sections methods - can you include those in the question? – jrturton Nov 21 '11 at 09:34
  • 4
    @jrturton Thanks for the heads up! Turns out I implemented rows in sections and sections in rows. Everything work fine after I switched them. Stupid mistake! – Simon Lee Nov 21 '11 at 09:37
  • @SimonLee you should accept David's answer then, he was correct. – jrturton Nov 21 '11 at 09:40
  • What the F :D Thanks buddy! – Vahid Mar 05 '17 at 13:10
15

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!

micnguyen
  • 1,419
  • 18
  • 25