objective-c:标签中只显示每个循环迭代的最后一个值
解析objective-c:标签中只显示每个循环迭代的最后一个值,objective-c,foreach,Objective C,Foreach,解析json后得到的名称计数是随机的,即它可以显示从1到100的任意数量的值。我创建了许多标签,如下面的代码所示,但是当我将NSString*name传递给putLabelsInScrollView方法时,label中只显示最后一次迭代的值。有谁能帮我解决这个逻辑,在不同创建的标签中显示不同的名称吗?我无法创建tableview,这本来很容易,稍后会更正标签和textfields的cGRects int i = 0; for(NSDictionary *myJsonD
json
后得到的名称计数是随机的,即它可以显示从1到100的任意数量的值。我创建了许多标签,如下面的代码所示,但是当我将NSString*name
传递给putLabelsInScrollView
方法时,label
中只显示最后一次迭代的值。有谁能帮我解决这个逻辑,在不同创建的标签中显示不同的名称吗?我无法创建tableview
,这本来很容易,稍后会更正标签和textfields的cGRects
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];
}
inti=0;
for(myJsonArray中的NSDictionary*myJsonDictionary)
{
//UILabel*标签=(UILabel*)[arrayLabel对象索引:i++];
//[label setText:myJsonDictionary[@“Name”];
NSUserDefaults*defaultNames=[NSUserDefaults standardUserDefaults];
NSString*name=myJsonDictionary[@“name”];
[defaultNames setObject:name forKey:@“QUESTIONNAME”];
NSLog(@“值为%@\n”,名称);
i++;
}
NSLog(@“每个循环的循环数=%d”,i);
[自编标签浏览:i];
-(void)putLabelsInScrollView:(int)numberOfLables
{
for(int i=0;i
试试这个
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;
}
试试这个
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;
}
试试这个
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;
}
试试这个
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;
}
将代码替换为以下内容:
获取结果:
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];
}
-(void)putLabelsInScrollView:(int)带文本的标签编号:(NSArray*)文本
{
for(int i=0;i
用以下代码替换您的代码:
获取结果:
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];
}
-(void)putLabelsInScrollView:(int)带文本的标签编号:(NSArray*)文本
{
for(int i=0;i
用以下代码替换您的代码:
获取结果:
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];
}
-(void)putLabelsInScrollView:(int)带文本的标签编号:(NSArray*)文本
{
for(int i=0;i