【问题标题】:UICollectionView does not scroll smoothlyUICollectionView 滚动不流畅
【发布时间】:2018-01-30 07:11:40
【问题描述】:

我的项目中有一个集合视图,其中包含来自 Web 服务的大量数据。当我垂直滚动集合视图时,它不会平滑滚动。

我使用SDWebImage 异步加载图像。

这里是cellForItemAtIndexPath的代码:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryListCell" forIndexPath:indexPath];

    cell.movName.text = [nameArray objectAtIndex:indexPath.item];

    num = [[numArray objectAtIndex:indexPath.item]integerValue];
    cell.itemNo.text = [NSString stringWithFormat:@"%ld", (long)num];


    [cell.movImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imageArray objectAtIndex:indexPath.item]]] placeholderImage:[UIImage imageNamed:@"Main_Logo.png"]];
    [cell.movImage setShowActivityIndicatorView:YES];
    [cell.movImage setIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    return cell;
}

当我在模拟器上运行项目时,它滚动顺畅,但在物理设备上运行时,它滚动不顺畅。我该如何解决这个问题?

【问题讨论】:

  • 我建议你使用时间分析仪仪器
  • 我认为你不需要在 cellForItemAt 方法中循环,只需使用 num = [[numArray objectAtIndex:indexPath.item]integerValue]; cell.itemNo.text = [NSString stringWithFormat:@"%ld", (long)num];没有任何 for 循环
  • 我已经使用了,但没有找到任何解决方案 Paul。
  • 尝试删除循环,检查@user1000 评论
  • 我使用了您的代码,但它在 iPad 中滚动不流畅。 @Reinier

标签: ios objective-c uicollectionview


【解决方案1】:

for loop 看起来像是罪魁祸首,基于它的大小。 SDWebImage 负责在后台线程上执行操作,所以应该没问题。

但是,对 collectionView:cellForRowAtIndexPath: 方法进行分析,并计算出渲染每个单元格所需的时间。如果超过 0.016 秒 - 这就是你的表现延迟的原因。以下是您可以快速用来衡量单元格渲染性能的代码。

NSDate *methodStart = [NSDate date];

CategoryListCell *cell = [[CategoryListCell alloc]init];
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CategoryListCell" forIndexPath:indexPath];

cell.movName.text = [nameArray objectAtIndex:indexPath.item];

for (int i=0; i<=numArray.count; i++)
{
    num = [[numArray objectAtIndex:indexPath.item]integerValue];
    cell.itemNo.text = [NSString stringWithFormat:@"%ld", (long)num];
}


[cell.movImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[imageArray objectAtIndex:indexPath.item]]] placeholderImage:[UIImage imageNamed:@"Main_Logo.png"]];
[cell.movImage setShowActivityIndicatorView:YES];
[cell.movImage setIndicatorStyle:UIActivityIndicatorViewStyleWhite];

NSDate *methodFinish = [NSDate date];
NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
NSLog(@"executionTime = %f", executionTime);

有关此方法的快速分析的更多详细信息,请查看:How to log a method's execution time exactly in milliseconds?

【讨论】:

  • 您可以发布一个 gif 或其他显示滚动性能的内容吗?您可以围绕这个 UICollectionView 发布更多代码吗?
【解决方案2】:

我想,你把行代码弄错了。因此,它在collectionView中不会平滑。您可以在后台设置它,或者您可以在函数 cellForItemAtIndexPath 之外计算它。

【讨论】:

    【解决方案3】:

    我真的没有看到太多问题。我想指出几件事。

    1. 您在 cellForItemAtIndexPath 中不必要地启动了集合视图单元格。 dequeueReusableCellWithReuseIdentifier: forIndexPath: 将始终返回一个单元格。

    2. cellForItemAtIndexPath 中的 for 循环有什么作用?它正在设置 itemNo text numArray 计数次数。我看错了吗?

    3. movImage 中有圆角吗?然后尝试不使用圆角,看看是否可以看到差异。渲染圆角很重。

    【讨论】:

    • 是的。我删除了 for 循环,movImage 没有圆角。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多