不能在目标C中正确绘制网格

时间:2012-10-04 12:05:41

标签: objective-c ios core-data grid

我在计算模块时遇到问题。我正在做以下事情。

- (void)makeGrid:withData:(NSDictionary * )data
{
    NSLog(@"aantal is: %d",[data count]);

    int xStart = 0;
    int yStart = 0;
    int xCurrent = xStart;
    int yCurrent = yStart;



    int xStepSize = 165;
    int yStepSize = 251;

    int xCnt =  3  ;
    int yCnt = [data count] % 3;

    int cellCounter = 0;

    UIView * gridContainerView = [[UIView alloc] init];
    [keeperView addSubview:gridContainerView];

    for (int y = 0; y < yCnt; y++) {
        for (int x = 0; x < xCnt; x++) {
            printf("xCurrent %d  yCurrent %d \n", xCurrent, yCurrent);
            NSString *url1 = @"player_imgUrl";
            NSString *url2 = [NSString stringWithFormat:@"%i", x];

            NSString *url3 = [url1 stringByAppendingString:url2];
            NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[data objectForKey:url3]]];
            UIImage* myImage = [[UIImage alloc] initWithData:imageData];

            UIImageView * myView = [[UIImageView  alloc] initWithImage:myImage];
            CGRect rect = myView.frame;
            rect.origin.x = xCurrent;
            rect.origin.y = yCurrent;
            myView.frame = rect;
            myView.tag = cellCounter;
            [gridContainerView addSubview:myView];

            //just label stuff
            UILabel * myLabel = [[UILabel alloc] init];

            [gridContainerView addSubview:myLabel];
            //--------------------------------

            xCurrent += xStepSize;
            cellCounter++;
        }

        xCurrent = xStart;
        yCurrent += yStepSize;
    }

    CGRect repositionRect = gridContainerView.frame;
    repositionRect.origin.y = 100;
    gridContainerView.frame = repositionRect;

}

我的NSLog说我的数据对象是16个值。当我运行它时,它只显示3个图像视图。有人知道我做错了吗?

请帮忙,

亲切的注册。

2 个答案:

答案 0 :(得分:0)

阅读完代码后,影像浏览量应为(xCnt * yCnt)。

你的问题在这里:

int yCnt = [数据计数]%3;

当您的数据为16时yCnt为1,这就是您的结果仅为3个图像视图的原因。

要解决此问题,您应该执行以下操作:

yCnt = (data.count / xCnt) + 1;

for (int y = 0 ; y < yCnt; y++)
{
    for (int x = 0; x < xCnt; x++)
    {
        if ((y == (yCnt - 1)) && (x > (data.count % xCnt)))
        {
            break;
        }
        else {
            // Your Grid code here
        }
    }
}

希望这有帮助。

答案 1 :(得分:0)

int yCnt = [data count] % 3;
当[数据计数] == 16;

时,

给出1