indexPath.row is always 0

10,942

Solution 1

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.

Solution 2

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!

Share:
10,942
Simon Lee
Author by

Simon Lee

Updated on June 10, 2022

Comments

  • Simon Lee
    Simon Lee about 2 years

    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.

  • Simon Lee
    Simon Lee over 12 years
    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:
  • jrturton
    jrturton over 12 years
    @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?
  • Simon Lee
    Simon Lee over 12 years
    @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!
  • jrturton
    jrturton over 12 years
    @SimonLee you should accept David's answer then, he was correct.
  • Vahid
    Vahid over 7 years
    What the F :D Thanks buddy!