1

我有一个重用单元格的 UITable。每个单元格都有一个取决于条件的图像。始终使用正确的图像,但是当重新使用单元格时,旧图像会显示在新图像后面。我曾尝试在添加新子视图之前删除 uiimageview,但它没有效果。

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *simpTableIden = @"simpTableIden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];

    }

    NSUInteger row = [indexPath row];

    cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:@"name"];

    CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0,   cell.frame.size.height, cell.frame.size.height);

    UIImage *greyDot = [UIImage imageNamed:@"greyDot.png"];
    UIImage *whiteDot = [UIImage imageNamed:@"whiteDot.png"];
    UIImageView *achievImg = [[UIImageView alloc] init];

    tableView.backgroundColor = [UIColor clearColor];

    achievImg.frame=picFrame;

    if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:@"achieved"] boolValue]) {

        achievImg.image = whiteDot;
    }
    else {
       achievImg.image = greyDot;
    }

    [achievImg removeFromSuperview];
    [cell.contentView addSubview:achievImg];

    return cell;
 }

任何帮助将不胜感激?

4

3 回答 3

3

问题是每次cellForRowAtIndexPath:调用你创建一个新的图像视图。我将您的代码更正如下:

在这里,我们为每个新单元格创建一次 imageView 并在每次cellForRowAtIndexPath:调用时替换它的图像:

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *simpTableIden = @"simpTableIden";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];
  UIImageView *achievImg = nil;
  if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];
    //New Cell ... Create Image view
    achievImg = [[UIImageView alloc] init];
    achievImg.tag = 10;
    [cell.contentView addSubview:achievImg];

  }
  else{
    //Old cell...get previously createdd imageview
    achievImg = (UIImageView*)[cell.contentView viewWithTag:10];
  }
  NSUInteger row = [indexPath row];

  cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:@"name"];

  CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0, cell.frame.size.height, cell.frame.size.height);

  UIImage *greyDot = [UIImage imageNamed:@"greyDot.png"];
  UIImage *whiteDot = [UIImage imageNamed:@"whiteDot.png"];


  tableView.backgroundColor = [UIColor clearColor];

  achievImg.frame=picFrame;

  if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:@"achieved"] boolValue]) {

    achievImg.image = whiteDot;
  }
  else {
    achievImg.image = greyDot;
  }

  //You don't need this because it doesn't have a superview in the first place..it's new
  //[achievImg removeFromSuperview];

  return cell;
}
于 2013-05-01T13:28:34.023 回答
0

您总是将 imageView 添加到单元格的 contentView 中。如果单元格被重用,将导致单元格中出现多个图像视图。

你可以给 imageView 一个标签。始终检查单元格的 contentView 中是否有视图将相同的标记。如果不添加 imageView。

UIImageView *achievImg = (UIImageView *)[cell.contentView viewWithTag:55];
if(!achievImg){
    achievImg = [[UIImageView alloc] init];
    achievImg.tag = 55;
    [cell.contentView addSubView:achievImg];
}
于 2013-05-01T13:26:15.590 回答
0

试试这样

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        static NSString *simpTableIden = @"simpTableIden";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpTableIden];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpTableIden];



        NSUInteger row = [indexPath row];

        cell.textLabel.text = [[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:@"name"];

        CGRect picFrame = CGRectMake(cell.frame.size.width-cell.frame.size.height, 0,   cell.frame.size.height, cell.frame.size.height);

        UIImage *greyDot = [UIImage imageNamed:@"greyDot.png"];
        UIImage *whiteDot = [UIImage imageNamed:@"whiteDot.png"];
        UIImageView *achievImg = [[UIImageView alloc] init];

        tableView.backgroundColor = [UIColor clearColor];

        achievImg.frame=picFrame;

        if ([[[self.achievements objectForKey:[keys objectAtIndex:row]] objectForKey:@"achieved"] boolValue]) {

            achievImg.image = whiteDot;
        }
        else {
           achievImg.image = greyDot;
        }

        [achievImg removeFromSuperview];
        [cell.contentView addSubview:achievImg];
       }
        return cell;
     }
于 2013-05-01T13:41:14.810 回答