解析后得到的名称计数json
是随机的,即它可以显示从 1 到 100 的任意数量的值。我创建了许多标签,如下面的代码所示,但只有最后一个迭代值显示在label
当我传递NSString *name
到putLabelsInScrollView
方法。任何人都可以帮我解决这个逻辑以在不同的创建标签中显示不同的名称吗?我无法创建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];
}