最喜欢的列表iOS 6位置数据

时间:2013-02-24 00:08:10

标签: objective-c ios6 favorites

我想从sqlite database加载我的数据并将其放在视图上(一张照片和说明)。

如何在从数据库加载数据后将数据放在X和Y位置?

我做了一个图片来解释它(我正在为iphone 5开发): enter image description here

我想也许我可以用CGRect来做,但我不确切知道如何。

2 个答案:

答案 0 :(得分:1)

我不确定如何在不给出代码作为答案的情况下解释。但作为一个正常的多维循环。计算位置并在当前子视图中添加新图像(如果能够这样做) 以下未经测试:

[编辑:更新了正确的缩进和语法错误]

   CGFloat IMAGE_WIDTH = 150;
   CGFloat IMAGE_HEIGHT = 150;
   CGFloat LABEL_PADDING = 10;

for (int i = 0; i < [TOTAL_ROWS]; i++) {
    // this is the initial frame for each row
    CGRect newFrame = CGRectMake(0, i * IMAGE_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT);
    for (int j = 0; j < [TOTAL_COLUMNS]; j++) {
        // but for column we need to more the x.position so its not stacking on itself
        newFrame = CGRectMake(IMAGE_WIDTH * j,
                              newFrame.origin.y,
                              IMAGE_WIDTH,
                              IMAGE_HEIGHT);

        UIImageView *newImageView = [[UIImageView alloc] initWithFrame:newFrame];
        newImageView.image = [UIImage imageNamed:@"image.png"];
         // create a label at the bottom of the image
        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(newFrame.origin.x,
                                                                      newFrame.origin.y +
                                                                      newFrame.size.height +
                                                                      LABEL_PADDING,
                                                                      newFrame.size.width,
                                                                      newFrame.size.height)];
        [newLabel setText:[NSString stringWithFormat:@"description %d", j]];
        NSLog(@"%@", NSStringFromCGRect(newFrame));
        [self.view addSubview: newImageView];
        [self.view addSubview: newLabel];
    }
}

答案 1 :(得分:0)

重要的是要说我使用了另一种更好的解决方案。

在iOS 6上,我们有一个名为CollectionViewController

的新东西

enter image description here

对于这个问题,这是一个非常好的解决方案。