1

I have 25 images in my array.so i need to display this 25 images in an view(any view) with a scroll functionality. However view will show the images as 5 rows. Each row need to hold 5 images. Scrolling need to be in vertical and also horizontal position(horizontal position is not mandatory). I searched the sample apps but didn't get it. so can any one help me to do it.any sample codes or links to refer?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Vipin
  • 4,718
  • 12
  • 54
  • 81

2 Answers2

0

Here you are,

-(void)setImagesInScrollView
{
    scrollView.delegate = self;

    int i=1;
    CGFloat cx = 0;
    CGFloat cy = 0;

    for(UIImageView *yourImgView in yourImgArray)
    {
        //Create Image
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
        imgView.contentMode = UIViewContentModeCenter;
        imgView.image = yourImgView.image;

        CGRect rect = imgView.frame;
        rect.size.height = imgView.frame.size.height;
        rect.size.width = imgView.frame.size.width;
        rect.origin.x += cx;
        rect.origin.y += cy;
        imgView.frame = rect;

        cx+=imgView.frame.size.width + 60;
        [scrollView addSubview:imgView];

        if(i!=1 && i%6==0)
        {
            cx=62;
            cy+=155;
        }

        [imgView release];
        i++;
    }

    [scrollView setContentSize:CGSizeMake(1024, cy+150)];
    [scrollView setFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, 1024, 630)];
    [scrollView setContentSize:CGSizeMake(1024, cy+150)];
    [scrollView setContentOffset:CGPointMake(0.0, 0.0)];
}

Above code was for iPad setting Images with 100 & 80 width & height respectively. . cx and cy variables are used to keep track for positioning next image. In 1 line here 6 Images are possible (Landscape mode). Modify code for the same as per your requirement.

If you need further help please leave a comment.

Hope this helps.

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
0

You kind of answered your own question just by saying "rows".

Why not use an UITableView with each UITableViewCell contentView having 5 UIImageView objects?

You could also have you horizontal scroll by adding an UIScrollView inside the cell, and then adding all images into the scrollview.

Felipe Sabino
  • 17,825
  • 6
  • 78
  • 112