【发布时间】:2014-08-08 03:08:17
【问题描述】:
我正在尝试创建这样的应用:
(来源:topmobiletrends.com)
每个视图都为 JSON 数组中的照片中的每个视图显示一个图像。
我已经成功设置并下载了图像数组,并将它们设置在UIImageView 中,并带有NSURLRequest 和SDWebImage。
我创建了一个带有图像视图的UIView,以测试我的ViewController(Storyboard) 中的代码,一切正常。但是,当我尝试创建一个UIView 以编程方式循环时,它显示为空白,没有ImageView。我不确定我做错了什么。
这是我的ViewController.m 代码
for (NSObject *photo in photos) {
UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(160,250,258,229)];
dView.backgroundColor = [UIColor whiteColor];
dispatch_async(dispatch_get_main_queue(),^{
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
myImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//Get image from url
[self.imageView setImageWithURL:imageURL];
[self.myImage addSubview:imageView];
priceLabel.text = [[[jsonDictionary objectForKey:@"site"] objectAtIndex:index] objectForKey:@"price"];
[imageView addSubview:dView];
});
}
}];
[task resume];
}
【问题讨论】:
-
为什么要为每张图片创建一个新的 UIView?
-
@raki 创建一个可重用的 xib(自定义 UIView)会更好吗?
-
一旦在下面观察我的答案......:)
标签: ios objective-c uiview uiimageview xib