如何以编程方式显示图像水平UIscrollView

我没有获取图像的Url.and然后显示在SCrollView.My这样的代码

NSMutableArray *al=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil]; for (NSDictionary *diction in al) { NSString *geting =[diction valueForKey:@"dealimage"]; NSLog(@"dealimage is %@",geting); NSData *getdata=[[NSData alloc]initWithData:[NSData dataFromBase64String:geting]]; NSLog(@"good day %@",getdata); dataimages=[UIImage imageWithData:getdata]; NSLog(@"dataimages %@",dataimages); [imagesarray addObject:dataimages]; } scrollView=[[UIScrollView alloc]init]; scrollView.delegate = self; scrollView.scrollEnabled = YES; int scrollWidth = 100; scrollView.contentSize = CGSizeMake(scrollWidth,100); scrollView.frame=CGRectMake(0, 0, 320, 100); scrollView.backgroundColor=[UIColor yellowColor]; [self.view addSubview:scrollView]; int xOffset = 0; imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]]; for(int index=0; index < [imagesName count]; index++) { UIImageView *img = [[UIImageView alloc] init]; img.bounds = CGRectMake(10, 10, 50, 50); img.frame = CGRectMake(5+xOffset, 0, 160, 110); NSLog(@"image: %@",[imagesName objectAtIndex:index]); img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]]; [images insertObject:img atIndex:index]; scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); [scrollView addSubview:[images objectAtIndex:index]]; xOffset += 170; } 

我的问题是ScrollView显示,但图像不添加所以请告诉我在我的代码有什么问题

现在我需要这个 图像2

我知道如何获取图像Url.now我需要显示图像水平ScrollView所以请给我任何想法的图像。

先谢谢了

你的编码是好​​的,但你分配的值[scrollView addSubview:[images objectAtIndex:index]]; ,这不是很好,我修改你的代码,根据你的结果想要

 UIScrollView *scrollVie=[[UIScrollView alloc]init]; scrollVie.delegate = self; scrollVie.scrollEnabled = YES; int scrollWidth = 100; //scrollVie.contentSize = CGSizeMake(scrollWidth,100); scrollVie.frame=CGRectMake(0, 51, self.view.frame.size.width, 100); scrollVie.backgroundColor=[UIColor redColor]; int xOffset = 0; for(int index=0; index < [imagesarray count]; index++) { UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(xOffset,10,160, 110)]; img.image = (UIImage*) [imagesarray objectAtIndex:index]; [scrollVie addSubview:img]; xOffset+=100; } [self.view addSubview:scrollVie]; scrollVie.contentSize = CGSizeMake(scrollWidth+xOffset,110); 

Apple创build的“官方”示例请参阅StreetScroller Demo。

对于图片中显示的所需结果,即水平滚动,请使用ScrollView而不是tableview。

将滚动窗格设置为水平,并在循环内dynamic创buildimageview并在其中插入图像

希望能帮助到你 :)

而不是使用表格视图,为什么不用UICollectionView与他们的代表。

你可以参考这个 – 以编程方式创build一个UICollectionView

这是达到你的要求的最好方法。