简体   繁体   English

TableView中可重用单元格上的indexPath.row号错误

[英]Wrong indexPath.row number on reusable cells in TableView

As you can see in the code below I have a tableView for times of racers. 如您在下面的代码中看到的,我有一个tableView来查看赛车手的时间。 First racer(s) have gold circle ( goldMedal image) second(s) have silver and third(s) bronze, everyone else has only medal image. 第一名拥有金圈(金goldMedal形象),第二名拥有银牌,第三名拥有铜牌,其他所有人只有medal形象。 Theese images are simply gold/silver/bronze/black circles. 这些图像只是金/银/青铜/黑眼圈。 I want to put an UILabel view on this medal image with number that will decribe raecer's order in the race, so racers with best (same) times will have goldMedal image with number 1 on it, second racer(s) will have silverMedal image with number 2 on it and so on. 我想在此奖牌图像上放置一个UILabel视图,该数字将描述种族在比赛中的raecer的顺序,因此,最佳(相同)时间的赛车手将拥有编号为1的goldMedal图像,第二名赛车手将获得带有silverMedal图像的数字2号,依此类推。 The problem is that when I have too many racers in tableview and I scroll down, racers don't have the same numbers. 问题是,当我在表视图中有太多赛车手并且向下滚动时,赛车手没有相同的数字。 That numbers are changing when scrolling. 滚动时这些数字正在改变。 I don't know what to do with that. 我不知道该怎么办。 Can anyone help me? 谁能帮我?

Also I used helpIndex variable to count numbers for racers that are 4. or worse, but now there is indexPath.row and none of theese 2 ways worked. 我还使用了helpIndex变量来为4或更差的赛车手计数,但是现在有indexPath.row并且这两种方式都没有起作用。 Can anyone give me at least his point of view how he would solve this problem? 谁能至少给我他的观点,他将如何解决这个问题? Thanks 谢谢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SKSTableViewCell";
    SKSTableViewCell *sksTableViewCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(sksTableViewCell.frame.origin.x+22.5, sksTableViewCell.frame.origin.y+20.5, 15, 15)];
    numberLabel.font = [UIFont systemFontOfSize:12];
    numberLabel.textColor = [UIColor whiteColor];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.adjustsFontSizeToFitWidth = YES;

    if (!sksTableViewCell) {
        sksTableViewCell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        sksTableViewCell.backgroundColor = [UIColor clearColor];
        if ([[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] times] count]) {
            sksTableViewCell.expandable = YES;
        } else {
            sksTableViewCell.expandable = NO;
        }
     }

     sksTableViewCell.imageView.image = [UIImage imageNamed:@"medal"];

     // Medals
     int golds = [[self.bestThreeRacersDictionary objectForKey:@"golds"] intValue];
     int silvers = [[self.bestThreeRacersDictionary objectForKey:@"silvers"] intValue];
     int bronzes = [[self.bestThreeRacersDictionary objectForKey:@"bronzes"] intValue];

     sksTableViewCell.textLabel.text = [NSString stringWithFormat:@"  %@",[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]];
     numberLabel.text = [NSString stringWithFormat:@"%lu", [Racer shift:golds silvers:silvers bronzes:bronzes]+indexPath.row];
     helpIndex++;
     if (indexPath.row < golds) {
         sksTableViewCell.imageView.image = [UIImage imageNamed:@"goldMedal"];
         numberLabel.text = @"1";
         helpIndex--;
     } else if (indexPath.row < (golds+silvers)) {
         sksTableViewCell.imageView.image = [UIImage imageNamed:@"silverMedal"];
         numberLabel.text = @"2";
         helpIndex--;
     } else if (indexPath.row < (golds+silvers+bronzes)) {
         sksTableViewCell.imageView.image = [UIImage imageNamed:@"bronzeMedal"];
         numberLabel.text = @"3";
         helpIndex--;
     }
     [sksTableViewCell addSubview:numberLabel];

     if ([[[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] times] count]) {
         sksTableViewCell.expandable = YES;
     } else {
         sksTableViewCell.expandable = NO;
     }
     sksTableViewCell.textLabel.text = [NSString stringWithFormat:@"%@", [[[ArraySingleton sharedManager].sharedArray objectAtIndex:indexPath.row] name]];

     return sksTableViewCell;
}

Instead of adding the numberLabel over and over again in cellForRowAtIndexPath, add it in your SKSTableViewCell: 不必在cellForRowAtIndexPath中一遍又一遍地添加numberLabel,而是将其添加到您的SKSTableViewCell中:

UILabel *numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(sksTableViewCell.frame.origin.x+22.5, sksTableViewCell.frame.origin.y+20.5, 15, 15)];
numberLabel.font = [UIFont systemFontOfSize:12];
numberLabel.textColor = [UIColor whiteColor];
numberLabel.textAlignment = NSTextAlignmentCenter;
numberLabel.adjustsFontSizeToFitWidth = YES;

The problem should be fixed. 该问题应得到解决。

sksTableViewCell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

You should pass your CellIdentifier here otherwise there is no point of checking cell exist or not. 您应该在此处传递CellIdentifier ,否则将无法检查单元格是否存在。 UITableView will always create new cell. UITableView将始终创建新的单元格。

My couple of notes which might help you: 我的一些笔记可能会帮助您:

  1. If you prefer, you can use different reusable identifiers parameter and based on identifier distinguish cells inside cell definition. 如果愿意,可以使用其他可重复使用的标识符参数,并根据标识符区分单元格定义内的单元格。

     -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
  2. You can create four different classes each subclass of table cell for Gold, Silver, Bronze and black and define everything in them and simply call them based on requirement. 您可以为表单元格的每个子类分别为Gold,Silver,Bronze和black创建四个不同的类,并定义它们中的所有内容,然后根据需要简单地调用它们。 That will reduce your overhead as you wont be doing this work over and over again while scrolling up and down. 这将减少您的开销,因为您不会在上下滚动时一遍又一遍地执行此工作。 Plus in future if you need to do more specific work for each cell it will be very convenient for you. 另外,将来如果您需要为每个单元做更多具体的工作,这对您来说将非常方便。

  3. Last thing having if else ladder is not good as well. 如果没有梯子,最后一件事也不好。 Code can go out of proportion very soon this way and reduces readability a lot. 这样,代码很快就会不成比例,从而大大降低了可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM