【发布时间】:2018-04-21 08:59:00
【问题描述】:
在我的 iOS 应用程序中,我有很多视图,图像和标签是的子视图。看法。我的问题是从 url 或远程服务器加载图像时,只有最后一个图像是从 url 加载的。其他图像使用占位符加载。尝试了很多方法,但无法解决。下面提供了我的代码和屏幕截图
catScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width,300)];
catScrollView.contentSize = CGSizeMake(([popularCategoryArray count] * 90)/1.8 + 10, catScrollView.frame.size.height);
[_parentScrollView addSubview:catScrollView];
catScrollView.backgroundColor=UIColorFromRGB(0xE8F7FE);
catScrollView.userInteractionEnabled=YES;
catScrollView.scrollEnabled=YES;
catScrollView.delegate=self;
for(int i =0;i<[popularCategoryArray count];i++)
{
Category* category=[popularCategoryArray objectAtIndex:i];
UIView *catView = [[UIView alloc] init];
if(i>=[popularCategoryArray count]/2+1)
{
catView.frame = CGRectMake(10 + y * 100, 4*10+90+10, 90, 120);
y++;
}
else
{
catView.frame = CGRectMake(10 + x * 100, 10, 90, 120);
x=i;
}
imageCat=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 90)];
imageCat.backgroundColor=[UIColor clearColor];
[imageCat sd_setImageWithURL:[NSURL URLWithString:category.ImageURL]
placeholderImage:[UIImage imageNamed:@"wish_list"]];
[catView addSubview:imageCat];
UILabel *labelCat=[[UILabel alloc]initWithFrame:CGRectMake(0, 95, catView.frame.size.width, 25)];
labelCat.backgroundColor=[UIColor clearColor];
labelCat.text=category.BngTitle;
labelCat.textAlignment=NSTextAlignmentCenter;
labelCat.font=[UIFont fontWithName:@"HelveticaNeue-Thin" size:12.0];
[catView addSubview:labelCat];
catView.backgroundColor=[UIColor clearColor];
[catScrollView addSubview:catView];
}
【问题讨论】:
-
Category 这是你的数组吗?
-
是的,类别的文本和图像 url 正在从数组中加载
-
您能分享该数组中包含图像的索引吗?或者请分享一个结果。
-
您的代码看起来正确,但我们不知道循环前后的
imageCat变量发生了什么。尝试将其作为UIImageView *imageCat = ...中的局部变量。还打印出带有NSLog("%@", category.ImageURL)的网址,并检查它们是否正确且功能正常。 -
为什么
imageCat是全球性的?它应该是局部变量。
标签: objective-c uiscrollview uiimageview sdwebimage uiscrollviewdelegate