使用NSArray中的UIButton自动填充scrollview

时间:2014-05-31 13:53:13

标签: ios objective-c ipad uibutton

我需要填充一个视图,使其成为UIButtons中NSArray对象的内容,并且如果按钮数量不适合要滚动的视图。例如,如果array.count为25且仅适合20个按钮,4列5行,则剩余5个按钮将通过滚动显示。

我目前正在测试它:

    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 728, 984)];
    sv.backgroundColor = [UIColor greenColor];
    sv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
sv.contentSize = CGSizeMake(PAGE_WIDTH, sv.frame.size.height);
sv.pagingEnabled = YES;
[self.view addSubview:sv]; // assuming a view controller

    NSMutableArray *freeHoursList = [NSMutableArray arrayWithObjects:
                                     [NSString stringWithFormat:@"10"],
                                     [NSString stringWithFormat:@"11"],
                                     [NSString stringWithFormat:@"12"],
                                     [NSString stringWithFormat:@"17"],
                                     [NSString stringWithFormat:@"18"],
                                     [NSString stringWithFormat:@"19"],
                                     [NSString stringWithFormat:@"10"],
                                     [NSString stringWithFormat:@"11"],
                                     [NSString stringWithFormat:@"12"],
                                     [NSString stringWithFormat:@"17"],
                                     [NSString stringWithFormat:@"18"],
                                     [NSString stringWithFormat:@"19"],nil];
for (int i = 0; i < freeHoursList.count; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.backgroundColor = [UIColor redColor];
btn.frame = CGRectMake(i * (BUTTON_WIDTH + BUTTON_PADDING), 10, BUTTON_WIDTH, BUTTON_HEIGHT);
btn.tag = i + MAGIC_BUTTON_TAG_OFFSET; // to relate to the array index
    NSString *numero = [freeHoursList objectAtIndex:i];
[btn setTitle:[NSString stringWithFormat:@"%@",numero]  forState:UIControlStateNormal];
//[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[sv addSubview:btn];

}

这就是我得到的:

enter image description here

1 个答案:

答案 0 :(得分:1)

我会添加跟踪实际Y位置和实际列的变量。因此,您将检查下一个按钮是否适合滚动的宽度,如果不是 - 将列设置为0并将X添加到按钮Y位置。它看起来像这样:

int BUTTON_Y = 10;
int column = 0;
for (int i = 0; i < freeHoursList.count; i++, column++)
{       
    if(column*(BUTTON_WIDTH + BUTTON_PADDING) + BUTTON_WIDTH > sv.contentSize.width) {
        BUTTON_Y += BUTTON_HEIGHT;
        column = 0;
    }

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.backgroundColor = [UIColor redColor];   
    btn.frame = CGRectMake(column * (BUTTON_WIDTH + BUTTON_PADDING), BUTTON_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
    btn.tag = column + MAGIC_BUTTON_TAG_OFFSET; // to relate to the array index
        NSString *numero = [freeHoursList objectAtIndex:i];
    [btn setTitle:[NSString stringWithFormat:@"%@",numero]  forState:UIControlStateNormal];
    //[btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sv addSubview:btn];
}