能够根据图片的数量自动显示对应的view
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/45bda28bb9ff86202c86102c9501ebc6.png)
代码实现
-(CGSize)photosSIzetocount:(int)count
{
int rows = 0;
if(count%3==0){
rows = count / 3;
}else{
rows = count / 3 + 1;
}
CGFloat photosH = rows * 75 + (rows - 1) * 10;
int cols = (count>2)?3:count;
CGFloat photosW = cols * 75 + (cols - 1) * 10;
return CGSizeMake(photosW, photosH);
}
-(void)setPhotosArray:(NSArray *)photosArray
{
_photosArray = photosArray;
int count = (int) photosArray.count;
while (self.subviews.count<count) {
UIImageView *imageV = [[UIImageView alloc] init];
[self addSubview:imageV];
}
for (int i=0; i<self.subviews.count; i++) {
UIImageView *photoV = self.subviews[i];
if(i<count){
photoV.hidden = NO;
NSDictionary *dict = _photosArray[i];
NSString *imageUrl = dict[@"thumbnail_pic"];
NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
photoV.image = [UIImage imageWithData:imagedata];
}else{
photoV.hidden = YES;
}
}
}
-(void)layoutSubviews
{
[super layoutSubviews];
int count = (int) _photosArray.count;
for (int i=0; i<count; i++) {
UIImageView *photoV = self.subviews[i];
int col = i%3;
CGFloat photoX = col * (75+10);
int row = i/3;
CGFloat photoY = row *(75+10);
photoV.frame = CGRectMake(photoX, photoY, 75, 75);
}
}