Hi, I have all the code written out to divide an NSMutableArray into sections for a UITableView. But in cellForRowAtIndexPath I am finding that indexPath.row returns only the row for the current section. Is there any way to simply provide the number of the row for the entire UITableView. If anyone needs to see the code I will attach it but I don't think it's very useful to answer this question except for if I must use another algorithm to find the cellForRow.
What if you just add a counter to the cellForRowAtIndexPath method? It should start at Section 0, Row 0 and get called for all rows/sections you've told it you have (in the numberOfRowsInSection and numberOfSectionsInTableView methods).
That should let you keep track of what row you are in for the overall table regardless of sections.
Yep, the row property of the indexPath is the row in the section, so you need to store an array of arrays (an array for each section). Something like:
// an array of arrays, one for each section
NSArray * _sectionDataArray;
When you build you index you create an array for each section and store it in the _sectionDataArray. To do this you need to build a NSMutableDictionary object, keyed by your section headers, containing an array of CellThing (an object containing your cell data). Once you have a dictionary you can just grab the array for each letter and stick it into _sectionDataArray.
In your cellForRowAtIndexPath method, you can access the cell data by getting the section array like this:
NSArray * sectionArray = [_sectionDataArray objectAtIndex:indexPath.section];
Once you have the section array you can access the individual cell data you are after by using the row in the indexPath:
CellThing * cellObject = [sectionArray objectAtIndex:indexPath.row];
Well obviously you have some way of knowing how many rows there are suppose to be for each section - You're supplying it in the tableview:rowsInSection: method.
For the sake of argument I'll assume there's a fixed number of rows in each section, so index in array would be indexPath.section * kNumOfRowsInSection + indexPath.row
If you really want to convert an index path to an absolute row number, you could do this:
// Start with row offset
int absoluteRow = indexPath.row;
// Add count of rows from all earlier sections
int s;
for (s = 0; s < indexPath.section; s++) {
absoluteRow += [tableView numberOfRowsInSection:s];
}
But it's probably better to organize your data to match your table structure.