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
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];
Use this code to get correct row,
NSUInteger row = 0;
for (NSUInteger i = 0; i < noofsectionsinTableView; i++)
row += [self.tableView numberOfRowsInSection:i];
return row;
精彩评论