如何在iOS中创建具有动态值的UIButtons网格?

ico*_*des 2 objective-c uibutton ios

我正在尝试创建一个UIButtons网格.行和列值是动态的.我知道如何创建网格.但是使用动态值制作网格是个问题.

 -(void)makeLayoutWithMinRow:(int)minRow maxRow:(int)maxRow minColumn:(int)minColumn maxColumn:(int)maxColumn {

    NSInteger intLeftMargin = 10; // horizontal offset from the edge of the screen
    NSInteger intTopMargin  = 10; // vertical offset from the edge of the screen
    NSInteger intYSpacing   = 30; // number of pixels between the button origins (vertically)
    NSInteger intXTile;
    NSInteger intYTile;

    NSInteger width;

    width = ((self.layoutView.frame.size.width-(maxColumn * 5))/maxColumn);
    for (int y = minRow; y < maxRow; y++)
    {
        for (int x = minColumn; x < maxColumn; x++)
        {
            intXTile = (x * width) + intLeftMargin;
            intYTile = (y * intYSpacing) + intTopMargin;
            UIButton *buttons[x][y] = [[UIButton alloc] initWithFrame:CGRectMake(intXTile, intYTile, width, 15)];
            //Here I get error : Variable-sized object may not be initialised.

            [self.layoutView addSubview:buttons[x][y]];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了下面Cornelius建议的选项,将按钮存储在数组中.

 sButton = [[UIButton alloc] initWithFrame:CGRectMake(intXTile, intYTile, width, 15)];
 [buttons addObject:sButton]; 
Run Code Online (Sandbox Code Playgroud)

在这种情况下如何添加这些按钮进行查看?

for (UIButton *obj in buttons) {
    [self.layoutView addSubview:obj];    //Doesn't work

}
Run Code Online (Sandbox Code Playgroud)

Sus*_*rma 5

这是替换的那个UICollectionView.我试过 PSTCollectionView,它给你预期的结果.试试这个.

你可以用PSTCollectionView.