2013-10-30 33 views
0

解析後得到的名稱計數json是隨機的,即它可以顯示範圍從1到100的任意數量的值。我創建了許多數目的標籤如下面的代碼所示,但只有最後一次迭代的值顯示在label當我通過NSString *nameputLabelsInScrollView方法。任何人都可以幫助我解決這個邏輯問題,以便在不同的創建標籤中顯示不同的名稱嗎?我無法創建tableview,這本來很簡單,稍後將糾正標籤的cGRectstextfieldsobjective-c:只有上次迭代的值爲 - 每個循環顯示在標籤

int i = 0; 
      for(NSDictionary *myJsonDictionary in myJsonArray) 
      { 
       //UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++]; 
       //[label setText:myJsonDictionary[@"Name"]]; 
       NSUserDefaults *defaultNames = [NSUserDefaults standardUserDefaults]; 
       NSString *name = myJsonDictionary[@"Name"]; 
       [defaultNames setObject:name forKey:@"QUESTIONNAME"]; 
       NSLog(@"Value is %@ \n", name);      
       i++; 
      } 
      NSLog(@"Number of cycles in for-each = %d", i); 
      [self putLabelsInScrollView:i]; 

- (void) putLabelsInScrollView:(int)numberOfLables 
{ 

     for(int i = 0 ; i < numberOfLables ; i++) 
     { 
      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)]; 
      [label setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0f]]; 
      label.numberOfLines = 2; 
      NSUserDefaults *defaultNameFetch = [NSUserDefaults standardUserDefaults]; 
      NSString *fetchedString = [defaultNameFetch objectForKey:@"QUESTIONNAME"]; 
      [label setText:[NSString stringWithFormat:@"%@", fetchedString]]; 
      //[label setText:myJsonDictionary[@"Name"]]; 

      [self.scroll addSubview:label]; 
      yPosition_label += 80; 

      UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)]; 
      text.borderStyle = UITextBorderStyleRoundedRect; 
      text.textColor = [UIColor blackColor]; 
      text.font = [UIFont systemFontOfSize:12.0]; 
      text.backgroundColor = [UIColor clearColor]; 
      text.keyboardType = UIKeyboardTypeDefault; 
      text.delegate = self; 
      [self.scroll addSubview:text]; 
      yPosition_text += 100; 
      yPosition_result = yPosition_label + yPosition_text; 
     } 
     [self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)]; 
     [self.view addSubview:self.scroll]; 
} 

回答

2

只是嘗試......

 for(NSDictionary *myJsonDictionary in myJsonArray) 
     { 
      NSString *name = myJsonDictionary[@"Name"]; 
      [self putLabelsInScrollView:name]; 
      NSLog(@"Value is %@ \n", name);      
     } 

     [self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)]; 
     [self.view addSubview:self.scroll]; 


     - (void) putLabelsInScrollView:(NSString *)labelText 
     { 


      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)]; 
      [label setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0f]]; 
      label.numberOfLines = 2; 
      [label setText:labelText]; 
      //[label setText:myJsonDictionary[@"Name"]]; 

      [self.scroll addSubview:label]; 
      yPosition_label += 80; 

      UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)]; 
      text.borderStyle = UITextBorderStyleRoundedRect; 
      text.textColor = [UIColor blackColor]; 
      text.font = [UIFont systemFontOfSize:12.0]; 
      text.backgroundColor = [UIColor clearColor]; 
      text.keyboardType = UIKeyboardTypeDefault; 
      text.delegate = self; 
      [self.scroll addSubview:text]; 
      yPosition_text += 100; 
      yPosition_result = yPosition_label + yPosition_text; 

     } 
+0

感謝噸iOS開發人員。 –

+0

不用擔心迪帕克。並隨意問任何幫助 –

1

這種替換代碼:

擷取結果:

int i = 0; 
    NSMutableArray *texts = [NSMutableArray array]; 
    for(NSDictionary *myJsonDictionary in myJsonArray) 
    { 
     //UILabel *label = (UILabel *)[arrayLabel objectAtIndex:i++]; 
     //[label setText:myJsonDictionary[@"Name"]]; 
     NSUserDefaults *defaultNames = [NSUserDefaults standardUserDefaults]; 
     NSString *name = myJsonDictionary[@"Name"]; 
     [texts addObject:name]; 
     NSLog(@"Value is %@ \n", name); 
     i++; 
    } 
    NSLog(@"Number of cycles in for-each = %d", i); 
    [self putLabelsInScrollView:i withTexts:texts]; 

和方法扣帽子文本

- (void) putLabelsInScrollView:(int)numberOfLables withTexts:(NSArray *)texts 
{ 

    for(int i = 0 ; i < numberOfLables ; i++) 
    { 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, yPosition_label, 261, 30)]; 
     [label setFont:[UIFont fontWithName:@"Helvetica Neue" size:12.0f]]; 
     label.numberOfLines = 2; 
     NSUserDefaults *defaultNameFetch = [NSUserDefaults standardUserDefaults]; 
     NSString *fetchedString = texts[i]; 
     [label setText:fetchedString]; 
     //[label setText:myJsonDictionary[@"Name"]]; 

     [self.scroll addSubview:label]; 
     yPosition_label += 80; 

     UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(10, yPosition_text, 261, 30)]; 
     text.borderStyle = UITextBorderStyleRoundedRect; 
     text.textColor = [UIColor blackColor]; 
     text.font = [UIFont systemFontOfSize:12.0]; 
     text.backgroundColor = [UIColor clearColor]; 
     text.keyboardType = UIKeyboardTypeDefault; 
     text.delegate = self; 
     [self.scroll addSubview:text]; 
     yPosition_text += 100; 
     yPosition_result = yPosition_label + yPosition_text; 
    } 
    [self.scroll setContentSize:CGSizeMake(self.scroll.frame.size.width, yPosition_result)]; 
    [self.view addSubview:self.scroll]; 
}