解析後得到的名稱計數json
是隨機的,即它可以顯示範圍從1到100的任意數量的值。我創建了許多數目的標籤如下面的代碼所示,但只有最後一次迭代的值顯示在label
當我通過NSString *name
到putLabelsInScrollView
方法。任何人都可以幫助我解決這個邏輯問題,以便在不同的創建標籤中顯示不同的名稱嗎?我無法創建tableview
,這本來很簡單,稍後將糾正標籤的cGRects
和textfields
。objective-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];
}
感謝噸iOS開發人員。 –
不用擔心迪帕克。並隨意問任何幫助 –