从单个方法实例化一定数量的唯一对象

发布于 2024-09-30 12:34:02 字数 1563 浏览 1 评论 0原文

这是我一直无法理解的事情……

假设我正在构建一个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-10-07 12:34:02

您的意思是:

for (int i=0;i<8;i++) {
    UIView* square = [[UIView alloc] initWithFrame:CGRectMake((i*x), (0*y), x, y)];
    [self addSubview:square];
    }

还是(对于所有行):

for(int r=0;r<8;r++) {
    for (int i=0;i<8;i++) {
        UIView* square = [[UIView alloc] initWithFrame:CGRectMake((i*x), (r*y), x, y)];
        [self addSubview:square];
        }
    }

Do you mean:

for (int i=0;i<8;i++) {
    UIView* square = [[UIView alloc] initWithFrame:CGRectMake((i*x), (0*y), x, y)];
    [self addSubview:square];
    }

Or (for all the rows):

for(int r=0;r<8;r++) {
    for (int i=0;i<8;i++) {
        UIView* square = [[UIView alloc] initWithFrame:CGRectMake((i*x), (r*y), x, y)];
        [self addSubview:square];
        }
    }
一念一轮回 2024-10-07 12:34:02
int numberOfRows = 8;
int numberOfColumns = 8;

for (int j = 0; j<numberOfRows;j++){
    for (int i=0;i<numberOfColumns;i++) {
        UIView* square = [[UIView alloc] initWithFrame:CGRectMake((i*x), (j*y), x, y)];
        [self addSubview:square];
    }
}
int numberOfRows = 8;
int numberOfColumns = 8;

for (int j = 0; j<numberOfRows;j++){
    for (int i=0;i<numberOfColumns;i++) {
        UIView* square = [[UIView alloc] initWithFrame:CGRectMake((i*x), (j*y), x, y)];
        [self addSubview:square];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文