UICollectionView 滚动性能不佳

Posted

技术标签:

【中文标题】UICollectionView 滚动性能不佳【英文标题】:UICollectionView poor scrolling performance 【发布时间】:2013-11-07 20:31:53 【问题描述】:

我遇到的问题是,每当一行出现在视野之外时,滚动都会有一个非常快且短暂的延迟。

这是我的代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)_collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

    UICollectionViewCell *cell = [self.collView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    Saints *note = nil;
    int aX=0;
    int aY=0;

       note = [self.fetchedResultsController objectAtIndexPath:indexPath];

    for (UIView *view in cell.contentView.subviews) 
        if([view isKindOfClass:[UIImageView class]])
        
            [view removeFromSuperview];

        
        else  if([view isKindOfClass:[UILabel class]])
        
              [view removeFromSuperview];

        


    cell.backgroundColor = [UIColor clearColor];
    UIImage *imgbackground = [UIImage imageNamed:@"today-left-side-images-bg.png"];

   NSArray *messages = [[NSArray alloc] initWithObjects: @"random-saint-text-bg-brown.png",@"random-saint-text-bg-brightblue.png",@"random-saint-text-bg-green.png",@"random-saint-text-bg-grey.png",@"random-saint-text-bg-lightblue.png",@"random-saint-text-bg-lime.png",@"random-saint-text-bg-purple.png",@"random-saint-text-bg-red.png",@"random-saint-text-bg-teal.png",nil];
    int randNum = arc4random() % [messages count];

    NSString *returnValue =  [messages objectAtIndex:randNum];


    NSString *rndBGImg= returnValue;
    UIImage *imgbackgroundlower = [UIImage imageNamed:rndBGImg];


    UIImageView *imgViewlower = [[UIImageView alloc] initWithImage:imgbackgroundlower];
    //imgViewlower.frame=CGRectMake(aX, aY+140, 140,73);
    imgViewlower.frame=CGRectMake(aX+4, aY+114, 119,43);


UIImageView *imgView = [[UIImageView alloc] initWithImage:imgbackground];

  imgView.frame=CGRectMake(aX, aY, 127,162);

    UIImageView *imgSaintPic = [[UIImageView alloc]init];
    imgSaintPic.tag=1;
    imgSaintPic.frame=CGRectMake(aX+6, aY+6, 114,145);
    //NSString *imgname = [aDic objectForKey:@"saint_image"];
    NSString *imgname = note.saint_imagethumbnail;
    UIImage *tempimg= [UIImage imageNamed:imgname];
    [imgSaintPic setImage:tempimg];

    UILabel *lblNamelocal=[[UILabel alloc]init];
    NSString *strSaintName=note.saint_name;
    //lblNamelocal.text=strSaintName;
    if([note.saint_title length]>1)
        lblNamelocal.text=[NSString stringWithFormat:@"%@ %@",note.saint_title,strSaintName];
    else
        lblNamelocal.text=[NSString stringWithFormat:@"%@",strSaintName];


    //lblNamelocal.frame = CGRectMake(aX+6, aY+109, 116,30);
    [lblNamelocal sizeToFit];
    lblNamelocal.frame = CGRectMake(aX+5, aY+115, 118,26);
    lblNamelocal.lineBreakMode = UILineBreakModeWordWrap;
    lblNamelocal.numberOfLines = 0;

    lblNamelocal.backgroundColor = [UIColor clearColor];
    lblNamelocal.textColor = [UIColor whiteColor];
    lblNamelocal.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:11 ];
    // lblNamelocal.textAlignment=UITextAlignmentLeft;
    lblNamelocal.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
    lblNamelocal.shadowColor = [UIColor darkGrayColor];
    lblNamelocal.shadowOffset = CGSizeMake(0,0.5);
    lblNamelocal.textAlignment = UITextAlignmentCenter;




    UILabel *lblsubtitle=[[UILabel alloc]init];
    //NSString *strSaintSubtitle=[aDic objectForKey:@"saint_title"];
    lblsubtitle.text=note.saint_subtitle;

    //lblsubtitle.frame = CGRectMake(aX+6, aY+132, 116, 25);
    lblsubtitle.frame = CGRectMake(aX+5, aY+140, 113, 20);
    lblsubtitle.lineBreakMode = UILineBreakModeWordWrap;
    lblsubtitle.numberOfLines = 2;


    lblsubtitle.backgroundColor = [UIColor clearColor];
    lblsubtitle.textColor = [UIColor whiteColor];
    lblsubtitle.font = [UIFont fontWithName:@"HelveticaNeue" size:10];
    // Removed CJamesRun lblsubtitle.textAlignment=UITextAlignmentLeft;
    lblsubtitle.shadowColor = [UIColor darkGrayColor];
    lblsubtitle.shadowOffset = CGSizeMake(0,0.5);
    lblsubtitle.textAlignment = UITextAlignmentCenter;

    imgViewlower.tag=3;
    lblNamelocal.tag=2;
    lblsubtitle.tag=4;
   [cell.contentView addSubview:imgView];
    [cell.contentView addSubview:imgSaintPic];
    [cell.contentView addSubview:imgViewlower];
    [cell.contentView addSubview:lblNamelocal];

    [cell.contentView addSubview:lblsubtitle];


  //  cell.layer.masksToBounds = NO;

    cell.layer.shouldRasterize = YES;
    cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

    return cell;

我厌倦的是栅格化单元格层,并在创建新单元格时从 SuperView 中删除视图。 还有其他建议吗?

【问题讨论】:

【参考方案1】:

我建议您只设置一次单元格的子视图,然后只更新collectionView:cellForItemAtIndexPath: 中的内容。例如。您在每个单元格中加载了一些图像,但每个单元格的图像都是相同的,因此每次重复使用单元格时都不需要加载它们。

【讨论】:

为了配合这个答案,他应该继承 UICollectionViewCell 以便它与他的视图一起设置。然后他可以设置适当的属性(图像视图的图像,标签的文本)。 谢谢。这是一个很好的补充,是处理收集单元的正确方法。

以上是关于UICollectionView 滚动性能不佳的主要内容,如果未能解决你的问题,请参考以下文章

Swift 中的 NSAttributedString draw(in: rect) 性能不佳?

UICollectionview 使用 CALayer 和 CATextlayer 滚动效果不佳

iOS5:UITableView 滚动性能不佳

加载滚动视图时性能不佳

jQuery 滚动事件 - 检测元素滚动到视图中 - Chrome 上的性能不佳

自 iOS 9 以来 UITableView 的滚动性能不佳(之前效果很好)