我正在尝试使用JSON
数据创建视图。JSON
数据中的标记名为"类型"
{
"type"=text;
};
OR
{
"type"=image;
};
我将创建一系列类型细节,并从该数组创建视图。**
float y=10;
for(ViewTypes *type in TypeArray )
{
if ([type.typeString isEqualToString:@"image"])
{
NSlog(@"image view is added");
UIImageView *imageView=[[UIImageView alloc]init ];
imageView.frame=CGRectMake(55, y, 150, 5);
imageView.backgroundColor=[UIColor yellowColor];
[CustomView addSubview:imageView];
y+=10;
}
else if ([type.typeString isEqualToString:@"text"]){
NSlog(@"text view is added");
UITextView *textView=[[UITextView alloc]init ];
textView.frame=CGRectMake(55, y, 150, 5);
textView.backgroundColor=[UIColor redColor];
[CustomView addSubview: textView];
y+=10;
}
}
`loop` and if-else is working fine ,But only `UITextView` is seen in the view and not the `UIImageView`.
答案 0 :(得分:0)
在UIImage
中添加UIImageView
以显示它。
if ([type.typeString isEqualToString:@"image"]){
NSlog(@"image view is added");
UIImageView *imageView=[[UIImageView alloc]init ];
imageView.frame=CGRectMake(55, y, 150, 5);
imageView.backgroundColor=[UIColor yellowColor];
**[imageView setImage:[UIImage imageNamed:@"imagename.png"]];**
[CustomView addSubview:imageView];
y+=10;
}
答案 1 :(得分:0)
试试这个,
float y=10;
for(ViewTypes *type in TypeArray ){
if ([type.typeString isEqualToString:@"image"]){
NSlog(@"image view is added");
UIImageView *imageView=[[UIImageView alloc]init ];
imageView.frame=CGRectMake(55, y, 150, 5);
imageView.backgroundColor=[UIColor yellowColor];
[CustomView addSubview:imageView];
[CustomView bringSubviewToFront:imageView];
y+=10;
}else if ([type.typeString isEqualToString:@"text"]){
NSlog(@"text view is added");
UITextView *textView=[[UITextView alloc]init ];
textView.frame=CGRectMake(55, y, 150, 5);
textView.backgroundColor=[UIColor redColor];
[CustomView addSubview: textView];
y+=10;
}}