2011-08-22 85 views
0

我试图将元素添加到使用此代码滚动视图:对象没有显示出来

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

什么也不表示在滚动视图向上。

回答

2

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

把这些放入并发布结果。

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

这也将是一个很好的想法来验证视图的帧在超视图内。 – NWCoder

1

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

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

你的while循环迭代了多少次?外部循环增加了标签的y位置。但标签只会显示在运行循环结束时/下一个运行循环的开始处。如果你用高y值的标签退出这种方法,那么你就不会看到它们。 (无论您在运行此代码时多少次更改y值,只有在全部完成时,显示器才会更新。)

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

(有很多的代码经过这里涉水。如果我的回答是这样了,你可能会得到更好的答案,但修剪回一些代码和隔离问题。)