我的UITableViewCell正在重用以前的单元格UIImage

时间:2013-05-01 13:15:32

标签: ios uitableview uiimageview

我有一个重用单元格的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;
 }

非常感谢任何帮助?

3 个答案:

答案 0 :(得分:3)

问题是,每次调用cellForRowAtIndexPath:时,您都会创建一个新的imageview。 我更正了您的代码如下:

这里我们为每个新的新单元格创建一次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;
}

答案 1 :(得分:0)

您始终将imageView添加到单元格的contentView中。如果重用该单元格将导致单元格中的多个imageView。

您可以为imageView指定标签。始终检查单元格的contentView中是否存在相同标记的视图。如果没有添加imageView。

UIImageView *achievImg = (UIImageView *)[cell.contentView viewWithTag:55];
if(!achievImg){
    achievImg = [[UIImageView alloc] init];
    achievImg.tag = 55;
    [cell.contentView addSubView:achievImg];
}

答案 2 :(得分: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;
     }