Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:12:07+00:00 2026-05-29T15:12:07+00:00

using the code down below to loop out 200 buttons and wnat the row

  • 0

using the code down below to loop out 200 buttons and wnat the row go down a notch when the row is full.
Im guessing there must be a better way because my way dosent work.

When the second and third row begins i onl have one button. No errors just buttons on each other on the last rows.

-(void)viewDidLoad {
int numba=0;
int x=-20;
int y=20;

for(int i = 1; i <= 200; ++i) {


    numba ++;


    if (numba <16) {

        x =x+20;

    } else if (numba >16 && numba <26){
        x=-20;
        x = x + 20;
        y=40;

    } else if (numba >26 && numba <36){
        x=-20;
        x =x+20;
        y=60;

    } else {
        x=-20;
        x =x+20;
        y=80;
    }



    UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(x, y, 20, 20);


    NSLog(@"numba = %d",numba);
    NSLog(@"x = %d",x);




    btn.tag = numba;
    [btn setTitle:[NSString stringWithFormat: @"%d", numba] forState:UIControlStateNormal];

    [self.view addSubview:btn];


    }

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:12:08+00:00Added an answer on May 29, 2026 at 3:12 pm
    1. When you want to create a 2-dimensional grid, it’s best to just use nested loops instead of trying to be clever with a single loop.

    2. Don’t sprinkle constant numbers all over your code. You can define symbolic constants in a method or function.

    Here’s how I’d do it:

    - (void)viewDidLoad {
        static const CGFloat ButtonWidth = 20;
        static const CGFloat ButtonHeight = 20;
        static const CGFloat RowWidth = 320;
    
        int buttonNumber = 0;
    
        for (CGFloat y = 0; buttonNumber < 200; y += ButtonHeight) {
            for (CGFloat x = 0; buttonNumber < 200 && x + ButtonWidth <= RowWidth; x += ButtonWidth) {
                ++buttonNumber;
                UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                button.frame = CGRectMake(x, y, ButtonWidth, ButtonHeight);
                button.tag = buttonNumber;
                [button setTtle:[NSString stringWithFormat:@"%d", buttonNumber] forState:UIControlStateNormal];
                [self.view addSubview:button];
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom drop down and using the code below it
I'm using code below to drag CEikRichTextEditor contents up and down (Scroll) on touch
I have written a cascading drop down list using JQuery. The code that I
Here's a version of the code I'm using, stripped down to just the parts
I am using the code below to put together my UITableView. The Cell height
I just cant get this working. I tried using the code below with onTouchEventand
i have created a coverflow using the code given by http://android.jmsliu.com/138/android-infinite-loop-gallery.html . And it
I'm using code that I found on the CodeProject.com for a low-level keyboard hook
I've recently started using code coverage tools (particularily Emma and EclEmma), and I really
I have been using code similar to this MessageDlg('', mtWarning, [mbOK], 0); throughout my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.