从单个方法实例化一定数量的唯一对象
这是我一直无法理解的事情……
假设我正在构建一个由 8 个 UIView 宽、8 个 UIView 高组成的网格。它可以由任意 x, y 集合组成。它不一定是 8 x 8。让我们任意选择并暂时坚持这一点。
这个 init 方法(UIView 子类的一部分)生成 8 行 UIViews 宽:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
int x = 1;
int y = 1;
//row 01
UIView* row01Square01 = [[UIView alloc] initWithFrame:CGRectMake((0*x), (0*y), x, y)];
[self addSubview:row01Square01];
UIView* row01Square02 = [[UIView alloc] initWithFrame:CGRectMake((1*x), (0*y), x, y)];
[self addSubview:row01Square02];
UIView* row01Square03 = [[UIView alloc] initWithFrame:CGRectMake((2*x), (0*y), x, y)];
[self addSubview:row01Square03];
UIView* row01Square04 = [[UIView alloc] initWithFrame:CGRectMake((3*x), (0*y), x, y)];
[self addSubview:row01Square04];
UIView* row01Square05 = [[UIView alloc] initWithFrame:CGRectMake((4*x), (0*y), x, y)];
[self addSubview:row01Square05];
UIView* row01Square06 = [[UIView alloc] initWithFrame:CGRectMake((5*x), (0*y), x, y)];
[self addSubview:row01Square06];
UIView* row01Square07 = [[UIView alloc] initWithFrame:CGRectMake((6*x), (0*y), x, y)];
[self addSubview:row01Square07];
UIView* row01Square08 = [[UIView alloc] initWithFrame:CGRectMake((7*x), (0*y), x, y)];
[self addSubview:row01Square08];
}
return self;
}
是否可以编写一个可以使用该代码(当然需要修改)的方法来生成后续 7 行 UIViews?更好的是,是否可以使用 1 行代码来生成所有 64 个 UI 视图?
我尝试过使用 for、while、do 循环,但我承认,当谈到是否可以将字符串作为参数传递给 init 方法时,我完全迷失了。
预先感谢您对此的任何见解。
This is something I've just never been able to get my brain around...
Let's say I am building a grid of 8 UIViews wide by 8 UIViews high. It could by any x, y set. It doesn't have to be 8 x 8. Let's just be arbitrary and stick to that for now.
This init method (part of a subclass of UIView) generates a row 8 UIViews wide:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
int x = 1;
int y = 1;
//row 01
UIView* row01Square01 = [[UIView alloc] initWithFrame:CGRectMake((0*x), (0*y), x, y)];
[self addSubview:row01Square01];
UIView* row01Square02 = [[UIView alloc] initWithFrame:CGRectMake((1*x), (0*y), x, y)];
[self addSubview:row01Square02];
UIView* row01Square03 = [[UIView alloc] initWithFrame:CGRectMake((2*x), (0*y), x, y)];
[self addSubview:row01Square03];
UIView* row01Square04 = [[UIView alloc] initWithFrame:CGRectMake((3*x), (0*y), x, y)];
[self addSubview:row01Square04];
UIView* row01Square05 = [[UIView alloc] initWithFrame:CGRectMake((4*x), (0*y), x, y)];
[self addSubview:row01Square05];
UIView* row01Square06 = [[UIView alloc] initWithFrame:CGRectMake((5*x), (0*y), x, y)];
[self addSubview:row01Square06];
UIView* row01Square07 = [[UIView alloc] initWithFrame:CGRectMake((6*x), (0*y), x, y)];
[self addSubview:row01Square07];
UIView* row01Square08 = [[UIView alloc] initWithFrame:CGRectMake((7*x), (0*y), x, y)];
[self addSubview:row01Square08];
}
return self;
}
Is it possible to write a method that could use that code (with modifications of course) to generate the subsequent 7 rows of UIViews? Better yet, is it possible to use 1 line of to generate all 64 UI Views?
I have experimented with using a for, while, do loop, but I admit I am completely lost when it comes to whether you or not you can pass a string as an argument to an init method.
Thanks in advance for any insight into this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的意思是:
还是(对于所有行):
Do you mean:
Or (for all the rows):