为 iOS 生成带有两个参数的按钮网格
Posted
技术标签:
【中文标题】为 iOS 生成带有两个参数的按钮网格【英文标题】:Generate grid of buttons with two parameters for iOS 【发布时间】:2012-05-07 10:49:33 【问题描述】:我需要为 ios 应用程序生成一个按钮网格。每个按钮需要两个参数:列数和行数。按钮也有两种状态,激活和停用。
加载应用时,我希望有 21 行和 16 列。屏幕上的某个地方还会有一个按钮,上面写着“添加列”,每次单击它都会添加 4 个额外的列。
任何建议我应该如何做到这一点?我可以先在 IB 中添加前 21*16 按钮,但这是否允许我稍后用额外的列来扩展它,以及如何?
编辑:赏金只是开始奖励 mbm30075,不需要新的答案
【问题讨论】:
【参考方案1】:我相信这是一个更完整的解决方案。这是我为测试而编写的 .h/.m 对:
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 设置是基本的:
View // tied to [ViewController view]
|
--->ScrollView // tied to [ViewController scrollView]
|
--->View // tied to [ViewController buttonsView]
|
--->UIButton // tied to [ViewController addButtons]
【讨论】:
非常感谢您写下如此完整的示例。我试试看 当然!让我知道事情的后续。它对我有用,但如果您需要任何调整,请告诉我。 addButtons 按钮的放置位置是否重要?我需要它与滚动视图处于同一级别(因此在滚动视图之外)。另外,addButtons
应该调用 addFourMoreColumns
操作,对吗?
哦,还有一个问题:为什么在滚动视图中需要另一个视图?按钮不能直接进入滚动视图吗?
每次我使用UIScrollView
或看到关于它的教程/演示时,它总是在其中托管另一个UIView
。然后,您设置[UIScrollView setContentSize:[UIView frame].size];
。这可能不是唯一的方法,但这是我一直这样做的方法。好问题。我今晚去看看!【参考方案2】:
这可以使用滚动视图UIScrollView
并使用两个for
循环在运行时添加所需按钮来实现,例如
for(int col = 0; col < [max column]; col++)
for(int row = 0; row < [max row]; row++)
//add buttons assigning them proper frame size
【讨论】:
【参考方案3】:您需要使用 2 个循环以编程方式添加按钮。还要识别按钮为它们分配标签,这些标签是由列号和行号组合而成的数字。 例如:- 第 1 行和第 1 列的按钮的标签可能为 11。 使用标签值,您可以识别单击了哪个按钮并执行相应的操作。
【讨论】:
以上是关于为 iOS 生成带有两个参数的按钮网格的主要内容,如果未能解决你的问题,请参考以下文章
Grid into Grid Popup Editor - 在子网格中传递 ID 参数