0

uibuttons を動的に作成し、そのボタンの x 座標と y 座標を設定する方法を説明しました。以下では、x と y のマージンは同じです。ボタンを正しく配置するために topLeft を次の「行」にインクリメントする方法と、ボタンを正しく配置するために topLeft を次の「列」にインクリメントする方法。

 static int width  = 100;
static int height = 37;
static int buffer = 8; // space between buttons (horiz. & vert.)
//static int marginX = 100;
//static int marginY = 200;
static int margin = 200;

 CGPoint topLeft = CGPointMake(margin,margin);
//CGPoint topLeft = CGPointMake(marginX,marginY); // 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])
 int count;
count=[langMA count];

for (int i = 0; i < count; 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:[inner1 objectForKey:@"text"] forState:UIControlStateNormal];

        //[btn setTitle:[NSString stringWithFormat:@"(%d,%d)", i + 1, j + 1] forState:UIControlStateNormal];
            // Add target actions...
            [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

            [[self view] 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);
    //topLeft = CGPointMake(topLeft.x + width + buffer, marginX);
}

// Redraw the view...
[[self view] setNeedsDisplay];
}
4

1 に答える 1

0

列の現在の X に変数を使用してみてください。このようなもの:

CGFloat currX = margin;

for (int i = 0; i < count; 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];

    currX += i * (width + 100);
    CGFloat currY = margin;


    // Now, we iterate through how many rows/buttons SHOULD exist ([self numRows])
    for (int j = 0; j < [self numRows]; j = j + 1) {
...
           [btn setFrame:CGRectMake(currX, currY, width, height)];
           currY += height + 20.0f;

お役に立てば幸いです。

于 2013-11-15T11:59:47.323 に答える