在滚动视图中创建动态UIButton的正确方法是什么?

4

我是iOS开发的新手。

我有一个基于导航的应用程序,在我的应用程序中,我使用for循环创建动态按钮。 在FirstViewController中,我有两个UITextField)。当用户输入的值,然后点击OK按钮的值将传递给anOtherViewController。在anOtherViewController中,我必须根据行和列的值放置一个逻辑来创建所有按钮。

我的逻辑代码:

for (int i = 1 ; i <= rows; i++)
{
    for (int j = 1 ; j <= columns ; j++)
    {
        NSString *btnTitle = [NSString stringWithFormat:@"%d",buttonCount];
        self.btnCount = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        self.btnCount.tag = [btnTitle intValue];
        [self.btnCount setTitle: btnTitle forState: UIControlStateNormal];
        [self.btnCount addTarget:self action:@selector(btnCountPressed:) forControlEvents:UIControlEventTouchUpInside];
        self.btnCount.frame = CGRectMake(162+changedX, 60+changedY, 43, 43);
        [self.scrollView addSubview:self.btnCount];

        [self.listOfbtnCount addObject:btnTitle];

        changedY = changedY + 50;
        buttonCount = buttonCount + 1;
    }
    changedX = changedX + 55;
    if (i == rows)
        widthScView = changedX;

    if (heightScView == 0)
        heightScView = changedY;

    changedY = 5;
}

我的屏幕截图:

输入图像描述

它运作良好,但我的问题是,如果我输入的的值超过了40大约),那么我的应用程序需要更长的时间来创建动态按钮。这个问题只与创建按钮所需的时间有关。

有没有办法更快地创建按钮?我还需要知道的是,我的代码是否有助于内存管理的问题?请在这些问题上帮助我。

附加信息:我没有生成错误,我只有一个耗费时间的按钮创建问题。

提前感谢您的帮助。

3个回答

7

你应该使用 UICollectionView 来实现这个功能。

UICollectionView 的使用方法和 UITableView 很像,它会管理在屏幕上显示的单元格,包括滚动,并在需要时向其数据源请求新单元格。你还可以回收单元格,所以你只需要创建足够显示的单元格加上一些额外的单元格。这样应该能大大提高性能,特别是在滚动时。

苹果公司有一些使用 UICollectionView 的示例代码,并且在2012 WWDC 视频中介绍 Collection Views也会让你有一个良好的开端。

我的问题是如果我输入的行和列的值大于 40

四十行四十列将会有 1600 个按钮,其中大多数在大部分时间里都是不需要存在的。通过为你管理需要在屏幕上显示的单元格,UICollectionView 将把此数字降低到大约 80 个(根据你的屏幕截图判断)。这样应该能看到更好的性能。

UICollectionView 还将简化按钮的定位,一旦你配置了集合视图(你可以在代码或界面生成器中进行配置),你不需要编写任何用于计算按钮位置的代码。


1
谢谢您的回复...但有关UICollectionView的更多信息或教程吗?我如何管理行和列的值?谢谢 :) - user1525369
你的回答很丰富,但是你还有其他选项吗?我也想知道对于我的情况来说,for循环是否适合我。 - user1525369
1
@RanjuPatel 我添加了参考页面链接、示例代码和一段应该有帮助的 WWDC 视频链接。 - Caleb
@RanjuPatel 是的,创建所有按钮的 for 循环是一个问题,因为你创建的按钮远多于用户一次能看到的数量,而且这个过程肯定耗时。如果将 UICollectionView 嵌入你的项目中,你根本不需要 for 循环了--集合视图会要求其代理 (通常是你的视图控制器)在需要时创建每个单元格 (即每个按钮)。 - Caleb
2
PSTCollectionView 可在 iOS5 上运行,并且与故事板兼容。没有理由不使用 UICollectionView。 - Paul de Lange
显示剩余2条评论

0

只需在这里实现您的逻辑和要求即可。

    int imageIndex = 0;

    int yOffset = 4;//set offset which you want...

    while (imageIndex < rows)        
    {
        int yPos =  7 + yOffset * 30; // set distance in height between two raws

        for(int i = 0; i < columns; ++i)
        {
            CGRect  rect = CGRectMake((0 + i * 80), yPos, 80, 31);**// here set frame of every button with different x and y postion// here width of button is 80 and height is 31**

            if (imageIndex < rows) {


                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = rect;
                button.tag = imageIndex;
                [button addTarget:self 
                           action:@selector(btnTemp_Clicked:)
                 forControlEvents:UIControlEventTouchDown];

                [button setTitle:[NSString stringWithFormat:@"%d",imageIndex] forState:UIControlStateNormal];
                [button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];

//                [button.titleLabel setTextColor:[UIColor blueColor]];
                [button setTitleColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"circle03.jpg"]] forState:UIControlStateNormal ];                
                [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted ];

                [button setNeedsDisplay];

                [yourScrollView addSubview:button];

            }
            ++imageIndex;
        }
        ++yOffset;
    }

看看我得到的输出屏幕..

enter image description here


问题出在哪里,伙计..?? :) - Paras Joshi
如果行数为150,列数为200,那么我遇到了与我的代码中相同的问题: - user1525369

0

尝试这个方法

    -(void)ScrollViewSetting {

        scrollview.scrollEnabled=YES;
        scrollview.userInteractionEnabled=YES;

        TotalStyleX=25;
        TotalStyleY=18;

        int count=0;
        int px=0;
        int py=0;


        for (int i=1; i<=TotalStyleX; i++) {
            px=0;
            for (int j=1; j<=TotalStyleY; j++) {
                count++;

                UIButton *btn1=[[UIButton alloc] init];

                btn1.tag = count;
                btn1.frame=CGRectMake(px+10, py+10, 300, 380); //you can set your height and width your x and y position
                [scrollview addSubview:btn1];

                px=px+320;     
            }
            py=py+400;

        }
        [scrollview setContentSize:CGSizeMake(px, py)];

    }

1
谢谢回复...请解释一下你的代码有什么新内容,以及它如何解决/帮助我的问题? - user1525369
你的TotalStyleXTotalStyleY数字是从哪里来的?根据OP的问题,不应该是“行”和“列”吗?即使如此,在大多数情况下这些按钮都不会同时可见时,你仍然会创建数百个按钮,这意味着有很多不必要的视图管理。 - Caleb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接