2

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

这是我的代码:

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

4

1 回答 1

2

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

于 2013-11-07T20:42:33.483 回答