Have been stuck on this (beginner) issue for a while so am now asking for help 🙂
In my UITableView, I’m showing a table with rows that contain: 1) Row #, 2) a person’s name, and 3) their score.
It looks something like this:
1. David 100
2. Arron 92
3. Cindy 90
4. Gina 85
5. Harry 85
6. Daniel 82
...
I’m generating this with this code in my UITableView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"PersonCell"];
cell.textLabel.text = [NSString stringWithFormat:@"%d. %@", indexPath.row+1,[topPersons objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [[topPersonsScore objectAtIndex:indexPath.row] stringValue];
return cell;
}
However, I’m trying to account for tied scores by duplicating row #s, something like:
1. David 100
2. Arron 92
3. Cindy 90
4T. Gina 85
4T. Harry 85
6. Daniel 82
...
so where Gina and Harry are now tied for 4th instead of being 4 and 5 even though they have the same score.
Is this something I can do in the cellForRowtIndexPath method? Or should I be doing this in another method that updates my topPersons NSMutableArray to include the row # / rank as part of the person’s name?
Any help would be appreciated – thank you!
I would not abuse the cell view or table view for that type of logic. I’d rather calculate the rank in another method and add the rank to the topPerson’s object.
cellForRowAtIndexPath is not called in any specific sequence. Nor are any of the cell view’s methods called in any particular sequence. And that makes it difficult to perform calculations like these.
You could of course determine whether the (indexPath.row-1)th topPersons object has the same value and in that case determine whether the (indexPath.row-2)th topPersons object has the same value too and in that case determine whether the (indexPath.row.2)th topPersons … and so forth. But I do not advise doing so.