gpt4 book ai didi

ios - UICollectionView 滚动性能差

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:09:43 25 4
gpt4 key购买 nike

我遇到的问题是,每当一行超出 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com