0

解析后得到的名称计数json是随机的,即它可以显示从 1 到 100 的任意数量的值。我创建了许多标签,如下面的代码所示,但只有最后一个迭代值显示在label当我传递NSString *nameputLabelsInScrollView方法。任何人都可以帮我解决这个逻辑以在不同的创建标签中显示不同的名称吗?我无法创建tableview本来很容易并且会在以后更正cGRects标签的内容textfields

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];
}
4

2 回答 2

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;

      }
于 2013-10-30T09:22:16.780 回答
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];
}
于 2013-10-30T09:29:51.107 回答