Pheepster
Pheepster

Reputation: 6347

Sectioned table view indexpath issue

I have a table view controller with all the US states listed in alphabetical sections. Clicking on a state returns detailed information about the state via a web service call. This is my first attempt at a sectioned or grouped table view and I am having trouble with the index path on rows that are past 'A'.

For instance, if I click on 'California' which is the first item in the 'C' group, the indexpath.row property is 0 instead of 4, which, since CA is the fifth state in alphabetical order, it should have an indexpath.row of 4.

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

    //get the letter in the current section
    NSString *letter = [stateIndex objectAtIndex:[indexPath section]];
    //get all the states beginning with the letter
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", letter];
    NSArray *states = [stateKeys filteredArrayUsingPredicate:predicate];
    if([states count] > 0){
        //get relevant state from states object
        NSString *cellValue = [states objectAtIndex:indexPath.row];
        [[cell textLabel] setText:cellValue];        
    }    
    return cell;
}

and in my seque, setting a break point on the last line in this method reveals that itemRowIndex is incorrect (except, of course, for the 'A' group):

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"sgShowStateRivers"]){
        RiversByStateTableViewController *riversVC = [segue destinationViewController];
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSInteger itemRowIndex = indexPath.row;
        NSString *key = [stateKeys objectAtIndex:itemRowIndex];
        [riversVC setStateIdentifier:[statesDict objectForKey:key]];
    }
}

How would one have a grouped table view where the items were still numbered as they were in the original array? Thanks! V

Upvotes: 1

Views: 530

Answers (1)

jszumski
jszumski

Reputation: 7416

Your understanding of NSIndexPaths in the context of a UITableView is incorrect. The row property resets to 0 for each individual section in the table.

You have two easy options to move forward:

  • What you can do is make a 2-dimensional array (an "array of arrays") where the first level is for each section and the second level each row in a section. This lets you directly use row and section of the index path to make your array lookup.

  • In prepareForSegue:sender: you can loop over the sections and count up the rows until you get to indexPathForSelectedRow.section and then add indexPathForSelectedRow.row to that count. This will give you an index you can use to fetch the state info from your 1-dimensional array.

Upvotes: 1

Related Questions