0

特定のセルのみが画像を取得するように、if ステートメントを使用しています。私のテスト例では、画像を取得する必要があるセルは 1 つだけで、if ステートメントは 1 回だけ実行されます。if ステートメントは、ラベルのテキストも変更します。ラベルは正しく変更されていますが、特に上下にスクロールすると、画像が複数のセルに追加されます。他のセルに余分な画像を追加しないようにする方法。

UIImageView *imageView= [[UIImageView alloc]initWithFrame:CGRectMake(114,5, 122, 63)];
    if (condition) {
    [imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
                imageView.tag = 777;
                [cell addSubview:imageView];
                cell.titleLabel.text = [dict valueForKey:@"name"];
                cell.titleDescription.text = [dict valueForKey:@"summary"];
    } else {
                [[cell viewWithTag:777] removeFromSuperview];
    }
4

2 に答える 2

1

UITableViewCell はキャッシュされるため、常に新しい UIImageView を作成する代わりに、最初に UIImageView があるかどうかを確認します。

UIImageView * imageView = (UIImageView*)[cell viewWithTag:777];

if (condition) {

    if(!imageView) {
        imageView= [[UIImageView alloc]initWithFrame:CGRectMake(114,5, 122, 63)];
    }
    [imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"Placeholder.png"]];
    imageView.tag = 777;
    [cell addSubview:imageView];
    cell.titleLabel.text = [dict valueForKey:@"name"];
    cell.titleDescription.text = [dict valueForKey:@"summary"];
} else {
    [imageView removeFromSuperview];
}

1 つのセルに複数の imageView を追加していた可能性が高いため、removeFromSuperView は最初の 1 つだけを削除していました。

于 2013-07-23T09:45:59.547 に答える
0

セルの作成中にこのようにしてください

  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath
 {
      UITableViewCell *cell = [aTableVIew dequeueReusableCellWithIdentifier:@"cell"];
      if(cell == nil)
      {
           cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]autorelease];
         UIImageView *aImgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 40, 40)];
       aImgView.tag = 777;
       [cell addSubview:aImgView];
       [aImgView release];
     }

     //use your condition hear onwards to make changes
    if(indexPath.section == 0)
     {
        UIImageView *view = (UIImageView *)[cell viewWithTag:777];
        view.image = [UIImage imageNamed:@"peter.png"];

     }
    else if (indexPath.section == 1)
     {
          if(indexPath.row == 0 )
           {      
              UIImageView *view = (UIImageView *)[cell viewWithTag:777];
              view.image = nil;
           }
          else
          {
              UIImageView *view = (UIImageView *)[cell viewWithTag:777];
              view.image = [UIImage imageNamed:@"peter.png"];

          }

    }

 return cell;

}


要件に応じて変更

注:私はARCを使用していません

于 2013-07-23T09:47:13.513 に答える