views:

208

answers:

4

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.

A: 

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.

Ken Pespisa
`cellForRowAtIndexPath` is called on demand, whenever the table needs to load a cell, not all at once. The cells could be loaded and reloaded in any order, especially as the user scrolls up and down.
benzado
+2  A: 

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];
Cannonade
Don't prefix your member variable names with an underscore. Apple has reserved the right to those names!
benzado
@benzado Thanks Ben, I wasn't aware of that.
Cannonade
A: 

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

Ron Srebro
+1  A: 

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.

benzado
Because my data structure is limited by the shape of the incoming XML, this was the necessary solution for me. Don't forget, though, that numberOfRowsInSection: is the -2nd- label in the method name. Should be [tableView.dataSource tableView:tableView numberOfRowsInSection:s];
MikeyWard
You were right that the method call was incorrect, but instead of calling the data source's method I call the table view's method, just to spite you. :-)
benzado