richard Stephenson
richard Stephenson

Reputation: 507

indexpath of cells in grouped tableview

i have a table view, split into alphabetical sections, populated from array that has been filtered

how can i track the "true" indexpath of cells in a grouped tableview. instead of the count starting at 0 for each section.

i need to get this so i can access the right bit of data from an array

at the moment it works like this(just in case im not clear)

(section 0) row 0 row 1 row 2 (section 1) row 0 row 1 etc...........

i need to get it so i can get

section 0) row 0 row 1 row 2 (section 1) row 3 row 4

thanks in advance

Upvotes: 1

Views: 780

Answers (2)

Jens Willy Johannsen
Jens Willy Johannsen

Reputation: 764

Try this:

- (NSUInteger)indexFromIndexPath:(NSIndexPath*)indexPath
{
    NSUInteger index=0;
    for( int i=0; i<indexPath.section; i++ )
        index += [self tableView:self.tableView numberOfRowsInSection:i];

    index += indexPath.row;

    return index;
}

It assumes that this method is placed in a UITableViewController class. You can use it like this:

NSUInterger realIndex = [self indexFromIndexPath:indexPath];
id myObject = [dataArray objectAtIndex:realIndex];

Upvotes: 2

KingofBliss
KingofBliss

Reputation: 15115

Use this code to get correct row,

NSUInteger row = 0;
for (NSUInteger i = 0; i < noofsectionsinTableView; i++)
   row += [self.tableView numberOfRowsInSection:i];
return row;

Upvotes: 2

Related Questions