Objective-C:逻辑问题

时间:2011-03-09 01:22:43

标签: objective-c

嘿伙计们,我在这里遇到一点麻烦。我有一个视图,使网格显示。我的意思是,我有9个项目并设置为每行显示3个。导致3行。没关系。我不会理解,这就是为什么我总是在他们之间找到一个空间。有时会出现在线条中间。空间等于一行高。

检查代码:

NSInteger quantidadeDeVideos = [self.videosURL count];
NSInteger contadorDeVideos = 0;

NSInteger idLinha = 0;
NSInteger linha = 1;
NSInteger itemq = 0;

while (contadorDeVideos < quantidadeDeVideos) {

    float f;
    float g;

    // Set the lines

    if (itemq < 3) {
        itemq++;
    }
    else {
        itemq = 1;
        linha++;
    }

    // This makes the second line multiplies for 150;
    if (linha > 1) {
        g = 150;
    }
    else {
        g = 0;
    }


    // Ignore this, this is foi make 1,2,3. Making space between the itens.

    if (idLinha > 2) {
        idLinha = 0;
    }


    NSLog(@"%i", foi);

    float e = idLinha*250+15;
    f = linha*g;

    UIImageView *thumbItem = [[UIImageView alloc] init];
    thumbItem.frame = CGRectMake(e, f, 231, 140);

    UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"VideosItemBackground.png"]];
    thumbItem.backgroundColor = bkgColor;
    thumbItem.opaque = NO;

    [self.videosScroll addSubview:thumbItem];

    contadorDeVideos++;
    idLinha++;

}

结果应该是:

[] [] []
[] [] []
[] [] []

这就是我得到的:

[] [] []

[] [] []
[] [] []

谢谢大家!

1 个答案:

答案 0 :(得分:1)

linha为1时,g为0,使linha * g为0.对于后续行,g为150,使linha * g == 300对于第二次迭代(第一次跳过300次),之后每次增加150次。您不应每次都有条件地设置g,而应该将其设为常数150,然后使用(linha - 1) * g作为f的值,或者只需将linha设置为0。

如果您想了解如何自己发现问题:

  1. 问问自己,这里出了什么问题?

    • 矩形被绘制得太低了一行
    • 只有第一行
    • 之后才会发生
  2. 因此,我们查看负责绘制矩形的位置的行:

    thumbItem.frame = CGRectMake(e, f, 231, 140)
    
  3. 变量f是y坐标。这必须是搞砸了。我们来看看如何定义f

    f = linha*g;
    
  4. 好的,linha是行号,它只在循环中更改一次,没有任何条件逻辑。所以问题可能是g。让我们看看如何定义:

    if (linha > 1) {
        g = 150;
    }
    else {
        g = 0;
    }
    
  5. 嘿,g在第一次迭代后发生了变化 - 正是在我们的问题出现时。让我们看看linha*g的值是什么:

    1 * 0 = 0
    2 * 150 = 300 (+300)
    3 * 150 = 450 (+150)
    4 * 150 = 600 (+150)
    
  6. 啊哈 - 问题是在第一次迭代时将g设置为0会破坏模式。

相关问题