Ios UICollectionView无法平滑滚动

Ios UICollectionView无法平滑滚动,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我的项目中有一个集合视图,其中有大量来自web服务的数据。当我垂直滚动集合视图时,它不会平滑滚动 我使用SDWebImage异步加载图像 以下是cellForItemAtIndexPath的代码: -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { CategoryListCell *cell

我的项目中有一个集合视图,其中有大量来自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;
}

当我在模拟器上运行项目时,它会平滑地滚动,但在物理设备上运行时,它不会平滑地滚动。我怎样才能解决这个问题

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

但是,对方法
collectionView:cellforrowatinexpath:
进行分析,并计算出渲染每个单元格所需的时间。如果超过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);
NSDate*methodStart=[NSDate-date];
CategoryListCell*cell=[[CategoryListCell alloc]init];
cell=[collectionView dequeueReusableCellWithReuseIdentifier:@“CategoryListCell”forIndexPath:indexPath];
cell.movName.text=[nameArray objectAtIndex:indexPath.item];

对于(int i=0;i我认为,您将行代码错误为。因此,它在collectionView中不会平滑。您可以在后台设置它,也可以在函数cellForItemAtIndexPath之外计算它。

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

  • 您正在不必要地启动cellForItemAtIndexPath中的集合视图单元格。dequeueReusableCellWithReuseIdentifier:forIndexPath:将始终返回单元格

  • cellForItemAtIndexPath中的for循环做什么?它正在设置itemNo text numArray计数次数。我是否误读了

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


  • 我建议您使用时间分析器工具我认为您不需要cellForItemAt方法中的循环,只需使用num=[[Numaray objectAtIndex:indexPath.item]integerValue];cell.itemNo.text=[NSString stringWithFormat:@“%ld”,long)num];没有任何for loop我已经使用了,但没有找到任何解决方案Paul。尝试删除该循环,检查@user1000注释我使用了您的代码,但在iPad中无法平滑滚动。@Reinier您是否可以发布gif或显示滚动性能的内容,并且可以围绕此UICollectionView发布更多代码?是的。我可以loop和movImage没有圆角。