【问题标题】:Adding one image to specific UICell using if statement adds it to multiple UICells Objective-c使用 if 语句将一个图像添加到特定 UICell 将其添加到多个 UICell Objective-c
【发布时间】:2013-07-23 09:32:18
【问题描述】:

我正在使用 if 语句,以便只有特定的单元格获取图像。在我的测试示例中,只有一个单元格应该获取图像,并且 if 语句只运行一次。 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];
    }

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    UITableViewCell 被缓存了,所以不要总是创建一个新的 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];
    }
    

    您可能会在一个单元格中添加多个 imageView,因此 removeFromSuperView 只是删除了第一个。

    【讨论】:

      【解决方案2】:

      这样做 在创建单元格的过程中

        -(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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        • 1970-01-01
        相关资源
        最近更新 更多