2013-10-30 34 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]; 
}