作为子视图添加到scrollview的对象未显示

时间:2011-08-22 21:44:03

标签: iphone sdk for-loop while-loop scrollview

我正在尝试使用以下代码将元素添加到滚动视图中:

int missionCount;
[connection release];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSDictionary *missionsDict = [responseString JSONValue];
/*NSArray *luckyNumbers = [json objectWithString:responseString error:&error];*/
NSLog(@"user Info array is: %@", missionsDict);
//    NSDictionary *array = [luckyNumbers1 objectForKey:@"data"];
NSDictionary *missionsData;
missionsData = [missionsDict objectForKey:@"data"];
NSLog(@"missionsData is: %@", missionsData);
NSEnumerator *inner = [missionsData objectEnumerator];
missionsScroll.contentSize = CGSizeMake(768, 1005);

id value;
int badgeY1;
int badgeY2;
int badgeY3;
badgeY1 = 146;
badgeY2 = 188;
badgeY3 = 188;
while((value = [inner nextObject])) {
    NSLog(@"value is: %@", value);
    NSLog(@"progress is: %@", [value objectForKey:@"progress"]);
    NSLog(@"user Info array is: %@", missionsDict);
    NSLog(@"name is: %@",[value objectForKey:@"reward_definitions"]);
    NSLog(@"missionsData is: %@", missionsData);
    NSDictionary *moreData;
    moreData = [value objectForKey:@"reward_definitions"];
    NSEnumerator *inner2 = [moreData objectEnumerator];
    id value2;
    int badgeX;
    int badgeCount;
    badgeX = 0;
    badgeCount = 0;
    while((value2 = [inner2 nextObject])) {
        UIProgressView *progressView;
        progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(323, badgeY1, 372, 9)];
        float progressValue;
        progressValue = ([[[value objectForKey:@"progress"] objectForKey:@"earned"] floatValue] / [[[value objectForKey:@"progress"] objectForKey:@"possible"] floatValue]);
        NSLog(@"progressValue is: %f", progressValue);
        [progressView setProgress:progressValue];
        [missionsScroll addSubview:progressView];
        UILabel *missionName;
        missionName = [[UILabel alloc] initWithFrame:CGRectMake(66, badgeY1, 227, 21)];
        missionName.backgroundColor = [UIColor clearColor];
        missionName.textColor = [UIColor whiteColor];
        missionName.font = [UIFont fontWithName:@"Heiti TC" size:23.0];
        missionName.text = [value objectForKey:@"name"];
        [missionsScroll addSubview:missionName];

        UILabel *requirementsLabel;
        requirementsLabel = [[UILabel alloc] initWithFrame:CGRectMake(66, badgeY2+25, 227, 21)];
        requirementsLabel.backgroundColor = [UIColor clearColor];
        requirementsLabel.textColor = [UIColor whiteColor];
        requirementsLabel.font = [UIFont fontWithName:@"Papyrus" size:19];
        requirementsLabel.text = @"To complete you need:";
        [missionsScroll addSubview:requirementsLabel];

        NSLog(@"Image URL is: %@", [value2 objectForKey:@"image_url"]);
        NSURL *url1 = [NSURL URLWithString: [NSString stringWithFormat:@"%@", [value2 objectForKey:@"image_url"]]];            
        NSData *urlData1 = [NSData dataWithContentsOfURL:url1];
        UIImage *image1 = [UIImage imageWithData:urlData1];
        UIImageView *badge = [[UIImageView alloc] initWithImage:image1];
        [badge setFrame:CGRectMake(badgeX, badgeY2+70, 70, 70)];               
        [missionsScroll addSubview:badge];
        [badge release];
        badgeCount = badgeCount+1;
        NSLog(@"badgeCount is: %i", badgeCount);
        if (badgeCount == 4) {
            NSLog(@"Badge Count = 4");
            badgeY2 = badgeY2 +70;
            badgeX = 0;
            badgeCount = 0;
        } else {
            NSLog(@"Badge Count ≠ 4");
            badgeX = badgeX +75;
        }

    }
    NSLog(@"1st While loop done");

    // NSLog(@"reward_definitions is: %@", [missionsData objectForKey:@"id"]);
    //       NSLog(@"Image URL is: %@", [[value objectForKey:@"reward_definitions"] objectForKey:@"image_url"]);
    //if ( [array isKindOfClass:[NSDictionary class]] ) {
    badgeY1 = badgeY1 +200;

    badgeY2 = badgeY2 +200;

    badgeY3 = badgeY3 +200;
    missionCount = missionCount+1; 
}
NSLog(@"While loops done");

for (int a; missionCount > 4; a = a+1) {
    missionsScroll.contentSize = CGSizeMake(776, missionsScroll.contentSize.height+200);
}

滚动视图中没有显示任何内容。

3 个答案:

答案 0 :(得分:2)

发生的事情并不明显,但首先要检查的是视图的有效位置(不是nil)以及此代码在主线程上运行。

将这些内容放入并发布结果。

   NSLog(@"missionsScroll: %@", (missionsScroll==nil)?@"NIL":@"OK");
   NSLog(@"progressView: %@", (progressView==nil)?@"NIL":@"OK");
   NSLog(@"missionName: %@", (missionName==nil)?@"NIL":@"OK");
   NSLog(@"mainThread: %@", ([NSThread isMainThread])?@"OK":@"Background Thread");

答案 1 :(得分:1)

您的代码非常复杂且难以阅读。也许您可以检查复杂的坐标计算是否按预期工作,例如

NSLog(@"Frame of badge %@", NSStringFromCGRect(badge.frame));

答案 2 :(得分:1)

你的while循环迭代了多少次?外环增加了标签的y位置。但标签只会在下一个运行循环的运行循环/开始结束时显示。如果您使用具有高y值的标签退出此方法,则您将看不到它们。 (在运行此代码时,更改y值的次数无关紧要。显示只会在完成后更新。)

**更正**您似乎每次在while循环中添加新视图。所以实际上我希望你在最终显示子视图时会出现多个子视图副本。

(这里有很多代码需要解决。如果我的回答很接近,你可能会得到更好的答案,但是会修改一些代码并隔离问题。)