【问题标题】:May I know why the tableview cell getting lazy while I'm scrolling?我可以知道为什么表格视图单元格在我滚动时变得懒惰吗?
【发布时间】:2023-03-21 00:10:01
【问题描述】:
if (cell.tag == indexPath.row) {
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[enclosureUrlArray objectAtIndex:indexPath.row]]
                           placeholderImage:[UIImage imageNamed:@"placeholder"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
                    CGSize itemSize = CGSizeMake(50, 50);
                    UIGraphicsBeginImageContext(itemSize);
                    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                    [cell.imageView.layer setMasksToBounds:YES];
                    [cell.imageView.layer setCornerRadius:2.5f];
                    [cell.imageView.image drawInRect:imageRect];
                    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    cell.imageView.autoresizingMask = UIViewAutoresizingNone;
       }

    return cell;

我只使用了 SDWebImage。当我滚动时,我的UITableView 单元格被卡住了 - 它滚动不顺畅。我正在从 Web 服务中检索图像。你能告诉我为什么会这样吗?

【问题讨论】:

  • 请更好地描述您的问题/问题。
  • 实际上,当我滚动它卡住的 tabelview 单元格时,它并不顺利。这是我的问题。我使用的上述代码。我从网络服务获取图像。
  • 图片在哪里?看起来您在单元格方法中做了很多工作,这就是它看起来滞后的原因。
  • 刚才我提到了图片大小是对的
  • 我认为如果 (cell.tag == indexPath.row) 尝试删除它,则无需设置条件。其实没用

标签: ios objective-c uitableview uiimage sdwebimage


【解决方案1】:

在您的代码中,您在滚动表格视图时会遇到问题,因为在下载图像后,您正在更改图像形状,例如应用边界、半径。因此,图像形状每次都在变化。最好不要使用任何 3rd 方库,而是使用 Apple API。

下面的代码可能会有所帮助:

cell.imageView.image = nil;
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
        dispatch_async(queue, ^(void) {

            NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:parsedData[@"imageLR"]];

            UIImage* image = [[UIImage alloc] initWithData:imageData];
            if (image) {
                 dispatch_async(dispatch_get_main_queue(), ^{
                     if (cell.tag == indexPath.row) {
                         cell.imageView.image = image;
                    CGSize itemSize = CGSizeMake(50, 50);
                    UIGraphicsBeginImageContext(itemSize);
                    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                    [cell.imageView.layer setMasksToBounds:YES];
                    [cell.imageView.layer setCornerRadius:2.5f];
                    [cell.imageView.image drawInRect:imageRect];
                    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();
                    cell.imageView.autoresizingMask = UIViewAutoresizingNone;
                         [cell setNeedsLayout];
                     }
                 });
             }
        });

【讨论】:

    【解决方案2】:

    如果您希望表格视图滚动得非常流畅,我建议您观看 WWDC 2012 的 Session 211,构建并发用户界面并在那里应用这些概念。这具有内容被独立查询和呈现的单元格。

    基本概念如下:
    1.在tableView:cellForRowAtIndexPath中,实例化了一个cell。
    2. 在同一方法中,创建检索数据以填充单元格的操作并将其存储到字典中。对单元格的引用被传递给操作。该操作有一个完成处理程序,用于填充单元格并从字典中删除该操作。
    3. 在方法返回单元格之前,将操作添加到操作队列中。
    4. 在tableView:didEndDisplayingCell:forRowAtIndexPath 中,对已移出屏幕的单元格的操作被取消并从字典中移除。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      相关资源
      最近更新 更多