【问题标题】:how to display images horizontal UIscrollView Programmatically如何以编程方式显示图像水平 UIscrollView
【发布时间】:2014-07-19 05:51:10
【问题描述】:

我确实为 Url 获取图像。然后在 SCrollView 上显示。我的代码是这样的

 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 是显示但没有添加图像所以请告诉我我的代码有什么问题

现在我需要这样的

我知道如何获取 Url 的图像。现在我需要显示图像 Horizo​​ntal ScrollView 所以请告诉我有关图像的任何想法。

【问题讨论】:

  • 你为什么对你得到的每一个答案都评论“请看一次我的问题”?
  • @AlexanderFarber 对不起,我的意图是我改变了问题,所以开玩笑地暗示了所有人

标签: ios objective-c uiscrollview uiimageview


【解决方案1】:

你的编码很好,但是你分配了[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);

【讨论】:

    【解决方案2】:

    Apple 创建的“官方”示例请看StreetScroller Demo。

    【讨论】:

    • 请看我的问题
    • @user3781721——这也是一个不错的答案
    【解决方案3】:

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

    将滚动窗格设置为水平并在循环内动态创建图像视图并在其中插入图像

    希望对你有帮助:)

    【讨论】:

    • @iosGure 感谢您的回复请我现在到此字段请给我任何有关您回答的链接
    • 查看 Apple 文档以获取滚动视图。这是另一个链接,您可以查看 linkbold
    • @iosGuru 请看一下我的问题
    • @LightYagami 请看一下我的问题
    【解决方案4】:

    您为什么不使用 UICollectionView 与其代表一起使用而不是使用表格视图。

    你可以参考这个 - Creating a UICollectionView programmatically

    这是满足您要求的最佳方式。

    【讨论】:

    • 感谢您的回复,我会尝试,但我不知道 UICollectionViewCell 上的 Scrollview 请给我任何相关链接
    • 非常欢迎,你为什么不通过这个 - UIcollection view in detail
    • 请看我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多