2011-08-22 48 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循環中。所以實際上我希望你有最終顯示的子視圖出現多個副本。

(有很多的代碼經過這裏涉水。如果我的回答是這樣了,你可能會得到更好的答案,但修剪回一些代碼和隔離問題。)