ios - UICollectionView 滚动性能不佳

标签 ios objective-c core-data uicollectionview uicollectionviewcell

我遇到的问题是,每当一行超出 View 时,滚动就会出现非常快且短的延迟。

这是我的代码:

- (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 中删除 View 。 还有其他建议吗?

最佳答案

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

关于ios - UICollectionView 滚动性能不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846095/

相关文章:

ios - 有没有办法弄清楚 NSManagedObjectContext 在哪个线程上?

iphone - iPad制作电子相册的这种控件哪里找

ios - 在 iOS 7 中使用相机时应用程序因内存压力而终止

ios - objective-c - 如何确保 block 在进一步执行任何其他代码之前执行?

objective-c - 多个 FQL 查询合二为一

ios - 多个数据模型的多个 ObjectContext?

iphone - Xcode 4.2 - '...' 的声明在此函数警告之外不可见

ios - instantiateViewControllerWithIdentifier - Storyboard ID 已设置但仍无法正常工作

iphone - 我如何加载我的 UITableView 以及我的文档目录中保存的文件列表?

iphone - 当被管理对象出现故障时,如何正确处理KVO通知?