草庐IT

ios - UICollectionView 滚动性能差

coder 2024-01-20 原文

我遇到的问题是,每当一行超出 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 - UICollectionView 滚动性能差的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  5. Ruby 的数字方法性能 - 2

    我正在使用Ruby解决一些ProjectEuler问题,特别是这里我要讨论的问题25(Fibonacci数列中包含1000位数字的第一项的索引是多少?)。起初,我使用的是Ruby2.2.3,我将问题编码为:number=3a=1b=2whileb.to_s.length但后来我发现2.4.2版本有一个名为digits的方法,这正是我需要的。我转换为代码:whileb.digits.length当我比较这两种方法时,digits慢得多。时间./025/problem025.rb0.13s用户0.02s系统80%cpu0.190总计./025/problem025.rb2.19s用户0.0

  6. ruby - Ruby 性能中的计时器 - 2

    我正在寻找一个用ruby​​演示计时器的在线示例,并发现了下面的代码。它按预期工作,但这个简单的程序使用30Mo内存(如Windows任务管理器中所示)和太多CPU有意义吗?非常感谢deftime_blockstart_time=Time.nowThread.new{yield}Time.now-start_timeenddefrepeat_every(seconds)whiletruedotime_spent=time_block{yield}#Tohandle-vesleepinteravalsleep(seconds-time_spent)iftime_spent

  7. ruby-on-rails - 如果条件与 &&,是否有任何性能提升 - 2

    如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:

  8. ruby - 如何找到我的 Ruby 应用程序中的性能瓶颈? - 2

    我编写了一个Ruby应用程序,它可以解析来自不同格式html、xml和csv文件的源中的大量数据。我如何找出代码的哪些区域花费的时间最长?有没有关于如何提高Ruby应用程序性能的好资源?或者您是否有任何始终遵循的性能编码标准?例如,你总是用加入你的字符串吗?output=String.newoutput或者你会使用output="#{part_one}#{part_two}\n" 最佳答案 好吧,有一些众所周知的做法,例如字符串连接比“#{value}”慢得多,但是为了找出您的脚本在哪里消耗了大部分时间或比所需时间更多,您需要进行分

  9. STM32的HAL和LL库区别和性能对比 - 2

    LL库和HAL库简介LL:Low-Layer,底层库HAL:HardwareAbstractionLayer,硬件抽象层库LL库和hal库对比,很精简,这实际上是一个精简的库。LL库的配置选择如下:在STM32CUBEMX中,点击菜单的“ProjectManager”–>“AdvancedSettings”,在下面的界面中选择“AdvancedSettings”,然后在每个模块后面选择使用的库总结:1、如果使用的MCU是小容量的,那么STM32CubeLL将是最佳选择;2、如果结合可移植性和优化,使用STM32CubeHAL并使用特定的优化实现替换一些调用,可保持最大的可移植性。另外HAL和L

  10. ruby - GC.disable 的任何性能缺点? - 2

    是否存在GC.disable会降低性能的情况?只要我使用的是真正的RAM而不是交换内存,就可以这样做吗?我正在使用MRIRuby2.0,据我所知,它是64位的,并且使用的是64位的Ubuntu:ruby2.0.0p0(2013-02-24revision39474)[x86_64-linux]Linux[redacted]3.2.0-43-generic#68-UbuntuSMPWedMay1503:33:33UTC2013x86_64x86_64x86_64GNU/Linux 最佳答案 GC.disable将禁用垃圾回收。像rub

随机推荐