【问题标题】:GIF imageview not display when tableview scroll back?表格视图向后滚动时不显示GIF图像视图?
【发布时间】:2019-04-01 22:46:39
【问题描述】:

我发现了一件很奇怪的事情。

1.我在我的 ViewController 中保留一个视图。

  1. 在 TableView 中添加此视图。

`

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    if(indexPath.row == 0)
    {
        [cell.contentView addSubview:self.imageContainer];
    }else
    {
        if(self.imageContainer.superview == cell.contentView)
        {
            [self.imageContainer removeFromSuperview];
        }
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        return 200;
    }
    else
    {
        return 44;
    }
}

当我滚动到底部并滚动回顶部时,gif 不显示。为什么?

【问题讨论】:

  • 请将代码发布为文本,而不是图像

标签: ios tableview cell gif


【解决方案1】:

只需更改如下一行代码即可解决您的问题:

来自

if (nil == _imageContainer)

if (nil == _imageContainer || _imageContainer.superview == nil)

如果只有一个容器,在数据源方法中,确保图像被突出显示以加载图像。希望它能解决你的问题。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
if(indexPath.row == 0)
{

    [cell.contentView addSubview:self.imageContainer];

UIImageView * imageView = (UIImageView *) _imageContainer.subviews[0]; imageView.highlighted = YES;

}else
{
    if(self.imageContainer.superview == cell.contentView)
    {

         [self.imageContainer removeFromSuperview];
    }
}
return cell;

}

【讨论】:

  • 谢谢!但我需要保存图像Container并将其添加到单元格中。我不想重新创建图像容器。这就是我需要的。这只是一个小演示。镜像Container里面大概有很多东西,创建一个消耗很大,所以我只想保留一个。
  • 你有多少个容器?我的意思是有多少细胞有这样的容器?
  • 有用!只有一个单元格有这样的容器。但是 imageContainer 可能有一个非常深的视图层次结构,并且每个层次结构都可能有图片。我需要找到所有图片并进行设置。这对我来说很奇怪。我想知道的是,为什么会这样?
  • 因为有一些代码可以加载 UIImage 的动画。这些代码不是由 addSubview(UIImageView 的父视图)执行的(即这里的 containerView)。如果设置了imageView.highlighted,则可以调用这些代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多