2011-03-09 36 views
1

嘿家伙,我在这里遇到了一些麻烦。我有一个使网格显示的视图。我的意思是,我有9个项目并设置每行显示3个。导致3行。没关系。我没有理解,这就是为什么我总能在他们之间找到空间。有时会出现在线条中间。该空间等于一行高度。Objective-C:逻辑问题

检查代码:

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

linha是1,g是0,使得linha * g 0。对于随后的行,g是150,使得linha * g == 300的第二次迭代(300在第一跳),在这之后增加了由每次150次。而不是每次都通过有条件地设置g,你应该使它成为一个常数150,然后要么使用(linha - 1) * gf值或只是在0

开始linha如果你想看看如何自己发现问题:

  1. 问问自己,这里怎么了?

    • 的矩形正在制定一个行过低
    • 这其中矩形绘制后只发生第一行
  2. 那么我们就来看看这是负责该行:

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

    f = linha*g; 
    
  4. OK,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) 
    

啊,哈 - 问题是,在第一次迭代设置g 0打破了格局。

+0

哦,他的工作!我只是没有看到这一点。非常感谢! – 2011-03-09 02:56:28

+0

+1很好的解释。 – 2011-03-09 05:17:14