0

已经在这个(初学者)问题上停留了一段时间,所以现在寻求帮助:)

在我的UITableView中,我展示了一个表格,其中的行包含:1) 行 #、2) 一个人的姓名和 3) 他们的分数。

它看起来像这样:

1. David     100
2. Arron     92
3. Cindy     90
4. Gina      85
5. Harry     85
6. Daniel    82
...

我在我的 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;
}

但是,我试图通过复制行#s来解释并列分数,例如:

1. David     100
2. Arron     92
3. Cindy     90
4T. Gina     85
4T. Harry    85
6. Daniel    82
...

所以吉娜和哈利现在并列第四而不是第四和第五,即使他们的分数相同。

这是我可以在cellForRowtIndexPath方法中做的事情吗?或者我应该在另一种方法中执行此操作,更新我topPersons NSMutableArray的行#/排名作为人名的一部分?

任何帮助将不胜感激 - 谢谢!

4

1 回答 1

2

对于这种类型的逻辑,我不会滥用单元格视图或表格视图。我宁愿用另一种方法计算排名并将排名添加到 topPerson 的对象。

cellForRowAtIndexPath 没有按任何特定顺序调用。也不是以任何特定顺序调用单元视图的任何方法。这使得执行这样的计算变得困难。

您当然可以确定 (indexPath.row-1)th topPersons 对象是否具有相同的值,在这种情况下确定 (indexPath.row-2)th topPersons 对象是否也具有相同的值,在这种情况下确定是否(indexPath.row.2)th topPersons ...等等。但我不建议这样做。

于 2012-11-16T08:56:31.947 回答