0

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

- (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 个图像视图。有人知道我做错了什么吗?

请帮忙,

亲切的问候。

4

2 回答 2

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
        }
    }
}

希望这可以帮助。

于 2012-10-04T12:42:35.953 回答
0
int yCnt = [data count] % 3;

当 [data count] == 16 时给出 1;

于 2012-10-04T12:43:48.530 回答