Correct way to create a dynamic UIButton in ScrollView?

I am new to iOS Development.

I have a navigation based application. In my application, I created dynamic buttons using for loop . I have two UITextField ( row and column ) in FirstViewController . When the user enters a row and column value, then click OK Button the row and column values . to anOtherViewController . In anOtherViewController I need to put the logic to create all buttons based on the values ​​of rows and columns.

MyLogical Code:

 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; } 

My screen:

enter image description here

It works fine, but my problem is that if I enter the row and column values more than 40 (about), then my application needs more time to create a dynamic button. The problem is only related to the time required to create the button.

Is there a way to create a button faster? and I also need to know if my code is bad for memory management? please help me on these issues.

For information: I have no generated errors, I only have a question about the time-consuming process of creating buttons.

Thanks at Advance.

+4
source share
3 answers

You must use a UICollectionView for this.

UICollectionView works just like a UITableView , because it will manage the cells that appear on the screen, including scrolling, and it will query its data source for new cells as needed. You can also recycle cells, so you will need to create enough to display, plus a few extra ones. This should really improve performance, especially when scrolling.

Apple has some sample code using the UICollectionView here and viewing the collection view view in 2012 WWDC Videos will help you get started well.

my problem is that if I enter the row and column values ​​over 40

Forty rows of forty columns will give you 1,600 buttons, most of which should not exist most of the time. By controlling which cells are needed on the screen for you, the UICollectionView will reduce this to about eighty (judging by your screenshot). Thus, you should see much greater performance.

UICollectionView also simplify button positioning, once you set up a collection view (which you can do in code or in Interface Builder), you don’t need any code to calculate button positions.

+7
source

just implement the logic here with your requirement.

  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; } 

See the screen I received.

enter image description here

0
source

Try this method

  -(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)]; } 
0
source

All Articles