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 回答