【问题标题】:uitableview - custom images keep loading in cell.contentViewuitableview - 自定义图像不断加载到 cell.contentView
【发布时间】:2010-09-22 20:06:45
【问题描述】:

cellForRowAtIndexPath 中,我将UIImageView 添加到cell.contentView。问题是,当单元格从屏幕上滚动并重新打开时,它会在已经存在的图像之上再次添加相同的图像。这种情况不断发生,直到我得到一个非常堆叠的模糊图像。

您是否必须继续删除您添加到cell.contentView 的任何图像视图?如果是这样,您在什么委托方法中执行此操作?

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

    static NSString *CellIdentifier = @"CellIdentifier";

    MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];

    imageView.center = CGPointMake(310, 48);
    [cell.contentView addSubview:imageView];
    [imageView release];
    return cell;
}

【问题讨论】:

  • 如果没有任何代码来看看发生了什么,这有点难以判断,但听起来你的问题可能与表格单元格重用有关,这是推荐的做法,通常由 XCode 模板实现.你能发布一些你的代码吗?
  • 只是出于好奇,如果您是从笔尖加载单元格,为什么不将图像添加到笔尖而不是通过编程方式进行呢?
  • 图片是动态的,是从服务器加载的,所以我不能把它放在笔尖里。
  • 哦,好的。您仍然可以将 UIImageView 添加到笔尖,然后通过设置“图像”属性以编程方式显示实际图像,但我想问题现在已经解决了 :)

标签: ios iphone uitableview


【解决方案1】:

如果您不想继续将 imageViews 放在单元格中,则必须在 if(cell==nil) 块内进行所有自定义,否则每次单元格回收时它都会添加一个。使用单元回收时,您总是希望为该块中的所有单元保留一致的任何内容,以便它们只添加一次。

例如:

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

    static NSString *CellIdentifier = @"CellIdentifier";

    MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        //Add custom objects to the cell in here!
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];

        imageView.center = CGPointMake(310, 48);
        [cell.contentView addSubview:imageView];
        [imageView release];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;


    return cell;
}

【讨论】:

    【解决方案2】:

    如果每个单元格都需要不同的图像,您可以尝试以下方法:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"CellIdentifier";
        static const int ImageViewTag = 1234; //any integer constant
    
        MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        UIImageView *imageView;
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
    
            //Add custom objects to the cell in here!
            imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, imgWidth, imgHeight)];
    
            imageView.center = CGPointMake(310, 48);
            imageView.tag = ImageViewTag;
            [cell.contentView addSubview:imageView];
            [imageView release];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        else
        {
            imageView = [cell viewWithTag:ImageViewTag];
        }
        imageView.image = yourUIImageForThisCell;
    
        return cell;
    }
    

    【讨论】:

    • 正如 filipe 在上面的评论中指出的那样,如果您首先将 UIImageView 添加到 nib 会更容易 - 然后您所要做的就是将 UIImageView 的 image 属性设置为 UIImage你要。我经常使用这种方法,我将它放在一个抽象类中,我的大多数表视图控制器都基于该类。
    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多