草庐IT

ios - 添加自定义按钮后 UIScrollView 运行不流畅

coder 2024-01-26 原文

我有一个 UIScrollView,其中有 500 多个自定义按钮,根据用户的要求,它最多可以达到 1000 个。实际上,是 UIButton 的子类。我使用 UIButton 类的原因是我必须在点击时执行一些操作。该对象看起来像下图

这些按钮的排列就像 m*n 矩阵,其中行 (m) 固定为 20,但列 (n) 不固定。可以有 500 列。

我的问题是添加按钮后 scrollView 运行不流畅。我使用以下代码来创建每个对象

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        sizeLabel = [[UILabel alloc] initWithFrame:sizeLabelRect];
        sizeLabel.font = [UIFont boldSystemFontOfSize:sizeLabelFontSize];
        sizeLabel.textAlignment = UITextAlignmentCenter;
//        sizeLabel.backgroundColor = [self colorWithHexString:LABEL_BG_COLOR];
//        sizeLabel.layer.cornerRadius = CORNER_RADIUS;
        [self addSubview:sizeLabel];

        nFilesLabel = [[UILabel alloc] initWithFrame:nFilesLabelRect];
        nFilesLabel.font = [UIFont boldSystemFontOfSize:nFilesFontSize];
        nFilesLabel.textAlignment = UITextAlignmentCenter;
//        nFilesLabel.backgroundColor = [self colorWithHexString:LABEL_BG_COLOR];
//        nFilesLabel.layer.cornerRadius = CORNER_RADIUS;
        [self addSubview:nFilesLabel];

        maxDisimilarity = [[UILabel alloc] initWithFrame:disimilarityRect];
//        maxDisimilarity.layer.cornerRadius = CORNER_RADIUS;
        [self addSubview:maxDisimilarity];

//        nFragment = [[UILabel alloc] init];
//        nFragment.font = [UIFont systemFontOfSize:nFragmentFontSize];
//        [self addSubview:nFragment];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)setObj:(CloneClass *)obj
{
    NSString *colorCode;
    if (obj != nil) {
        _obj = obj;
        if ([_obj.type isEqualToString:@"1"]) {
            colorCode = COLOR;
        } else if ([_obj.type isEqualToString:@"2"]) {
            colorCode = COLOR;
        } else if ([_obj.obj isEqualToString:@"Type-3"]) {
            colorCode = COLOR;
        }
        self.backgroundColor = [self colorWithHexString:colorCode];
        [self setTitle:[NSString stringWithFormat:@"%d", obj.nFragments] forState:UIControlStateNormal];

        // set size of clone class
        if (_obj.size > LARGE_SIZE) {
            sizeLabel.text = LARGE_SIZE_TEXT;
        } else if (_obj.size > MEDIUM_SIZE && _obj.size <= LARGE_SIZE) {
            sizeLabel.text = MEDIUM_SIZE_TEXT;
        } else {
            sizeLabel.text = SMALL_SIZE_TEXT;
        }

        // set number of files within a clone class
        nFilesLabel.text = [NSString stringWithFormat:@"%d", _cloneClass.nFiles];

        // set color for disimilarity
        CGFloat similarity = 1.0 - _cloneClass.maxDisimilarity;
        maxDisimilarity.backgroundColor = [UIColor colorWithRed:1.0 green:similarity blue:similarity alpha:1.0];

    } else {
        self.backgroundColor = [UIColor grayColor];
        [self setTitle:@"/" forState:UIControlStateNormal];
    }

    self.titleLabel.textColor = [UIColor blackColor];
}

ScrollView 甚至不适用于 10*10 矩阵。

提前致谢

最佳答案

我会查看 UICollectionView。你现在会遇到内存问题。 UICollectionView 允许您创建一个类似于 UITableViewCellUICollectionViewCell,可以重复使用。

参见 Apple Documentation .

关于ios - 添加自定义按钮后 UIScrollView 运行不流畅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940506/

有关ios - 添加自定义按钮后 UIScrollView 运行不流畅的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  3. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  4. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  5. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  6. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  7. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  10. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

随机推荐