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 7897415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:09:38+00:00 2026-06-03T08:09:38+00:00

I need to generate a grid of buttons for an iOS app. Each button

  • 0

I need to generate a grid of buttons for an iOS app. Each button needs two parameters: the number of the column and the number of the row. A button will also have two states, activated and deactivated.

When the app is loaded I want there to be like 21 rows and 16 columns. And somewhere on the screen there will also be a button that says “add columns” and this would add 4 extra columns every time it’s clicked.

Any suggestions how I should do this? I could start by adding the first 21*16 buttons with the IB but will that allow me to extend it with extra columns later on, and how?

Edit: the bounty was only started to reward mbm30075, no new answers necessary

  • 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-06-03T08:09:39+00:00Added an answer on June 3, 2026 at 8:09 am

    I believe this is a more complete solution. Here’s the .h/.m pair I wrote up for testing:

    ViewController.h

    #import <UIKit/UIKit.h>
    @interface ViewController : UIViewController 
    @property (assign, nonatomic) int numColumns;
    @property (assign, nonatomic) int numRows;
    @property (strong, nonatomic) NSMutableArray *buttonsArray;
    @property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (strong, nonatomic) IBOutlet UIButton *addButtons;
    @property (strong, nonatomic) IBOutlet UIView *buttonsView;
    - (IBAction)addFourMoreColumns;
    @end
    

    ViewController.m

    #import "ViewController.h"
    @implementation ViewController
    @synthesize addButtons;
    @synthesize buttonsArray;
    @synthesize buttonsView;
    @synthesize numColumns;
    @synthesize numRows;
    @synthesize scrollView;
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self setButtonsArray:[NSMutableArray array]];
        [self setNumColumns:16];
        [self setNumRows:21];
        [self layoutButtons];
    }
    - (void)layoutButtons {
        static int width  = 100;
        static int height = 37;
        static int buffer = 8; // space between buttons (horiz. & vert.)
        static int margin = 20;
        CGPoint topLeft = CGPointMake(margin,margin); // standard "top left" in iOS
        // Since you appear to be wanting to modify the number of columns,
        // I'm going to make the multi-dimension array an array of columns, 
        // with each column containing an array of buttons (representing the rows)
    
        // Iterate through how many columns SHOULD exist ([self numColumns])
        for (int i = 0; i < [self numColumns]; i = i + 1) {
            // Check if this "column" exists (does this index exist in [self buttonsArray]?)
            if (i >= [[self buttonsArray] count]) {
                // It doesn't exist, so we need to add a blank array
                [[self buttonsArray] addObject:[NSMutableArray array]];
            }
            NSMutableArray *column = [[self buttonsArray] objectAtIndex:i];
            // Now, we iterate through how many rows/buttons SHOULD exist ([self numRows])
            for (int j = 0; j < [self numRows]; j = j + 1) {
                // Check if this "row"/"cell"/"button" exists
                if (j >= [column count]) {
                    // It doesn't exist, so we need to add a new button AND PLACE IT!
                    // Of course, you need to make your button type correctly
                    // This is just standard button code...
                    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                    [btn setFrame:CGRectMake(topLeft.x,topLeft.y,width,height)];
                    // Do whatever else you need to do with the button...
                    // Set title...
                    [btn setTitle:[NSString stringWithFormat:@"(%d,%d)", i + 1, j + 1] forState:UIControlStateNormal];
                    // Add target actions...
                    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
                    // Add the button to the view
                    [[self buttonsView] addSubview:btn];
                    // Add the button to the array
                    [column addObject:btn];
                }
                // Increment topLeft to the next "row" for correct button placement...
                topLeft = CGPointMake(topLeft.x, topLeft.y + height + buffer);
            }
            // Increment topLeft to the next "column" for correct button placement...
            topLeft = CGPointMake(topLeft.x + width + buffer, margin);
        }
        // So, I'm assuming your "add columns" button will be placed equivalent to 
        // "(columnCount + 1, 1)", or at the top of the screen, just to the right
        // of the right-most column of buttons
        // [self addButtons] is the property to the UIButton that calls [self addFourMoreColumns]
        [[self addButtons] setFrame:CGRectMake(topLeft.x, topLeft.y, [[self addButtons] frame].size.width, [[self addButtons] frame].size.height)];
        // Now, update the view that holds the buttons to give it a new size (based on the buttons added)
        CGRect viewFrame = CGRectMake(0, 0, CGRectGetMaxX([[self addButtons] frame]) + buffer, 20 + ([[self buttonsArray] count] * (height + buffer)));
        [[self buttonsView] setFrame:viewFrame];
        [[self scrollView] setContentSize:viewFrame.size];
        // Redraw the view...
        [[self view] setNeedsDisplay];
    }
    - (IBAction)addFourMoreColumns {
        [self setNumColumns:[self numColumns] + 4];
        [self layoutButtons];
    }
    // Shows in the log which button you pressed: "(col, row)"
    - (void)buttonPressed:(id)sender {
        for (int i = 0; i < [[self buttonsArray] count]; i = i + 1) {
            NSMutableArray *col = [[self buttonsArray] objectAtIndex:i];
            for (int j = 0; j < [col count]; j = j + 1) {
                if (sender == [col objectAtIndex:j]) {
                    NSLog(@"button (%d,%d) pressed", i + 1, j + 1);
                    break;
                }
            }
        }
    }
    @end
    

    Xib setup is basic with:

    View                 // tied to [ViewController view]
    |
    --->ScrollView       // tied to [ViewController scrollView]
        |
        --->View         // tied to [ViewController buttonsView]
            |
            --->UIButton // tied to [ViewController addButtons]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate external links in admin interface grid column, but they shows
I need to generate invoices in large batch which will be transformed to EDI
I need a way to generate the header texts in the grid as sortalbe
So I have a Listbox: <ListBox Grid.Row=0 Grid.Column=1 Grid.RowSpan=2> //<----Item's Data Source Method Call
In either HTML5 or written natively in Objective-c, I need to generate a grid
I'm using the code in this answer to generate a grid of buttons. When
I need generate action links outside controllers. I can use Html.Action in Views, Url.Action
I need to generate an editable xml file to supply content to a flash
I need to generate Entities/Object from selected tables - not all. Is this possible
I need to generate the following in ASP.NET page. What's the best, easiest way

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.