2013-10-30 2 views
0

이름 카운트 내가 파싱 한 후 json은 무작위입니다. 즉, 1에서 100까지의 값을 임의로 표시 할 수 있습니다. 레이블을 아래 코드와 같이 나타내지 만 NSString *name에서 putLabelsInScrollView까지의 방법으로 마지막으로 반복 한 값만 label에 표시됩니다. 어느 누구도이 논리를 수정하여 다른 이름으로 다른 레이블을 표시하도록 도와 줄 수 있습니까? tableview을 쉽게 만들 수없고 cGRects 라벨을 수정하고 나중에 textfields을 수정합니다.objective-c : for-each 루프에서 반복되는 마지막 값만 라벨에 표시됩니다.

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]; 
} 
관련 문제