Q:

iOS creating a matrix of UIButtons

I'm loading preview Images (Thumbnails) from a server and saving them to the local documents directory. For each images there is a core data entry.
Now i need to render this Thumbnails into a ScrollView. I don't know how many thumbnails it will be in future, why i need to render the thumbs programmatically into the ScrollView. i Know that i have to set the height of the scrollView depending of the number of thumbnails too.
The Thumbnails need to be touchable, cause a tap on a thumbnail should open another dialogue.
First Question: What is the right Control to use for displaying the thumbnails? Is it the UIButton as a Custom Button with the thumbnail set as Background-Image?
Second Question: How can i set up a dynamic matrix to render the thumbs (buttons) to.
Thanks

A:

First answer: I would recommend using a UIButton for this purpose, this is what I would do in this situation
Second answer: Assuming you have an array of some sort of all of your thumbnails, then you can simply iterate over them do create all of the buttons in a manner similar to this:
NSArray *thumbnailImages;
UIScrollView *scrollView;
//Scrollview is linked up through IB or created dynamically...whatever is easier for you
//The thumbnailImages array is populated by your list of thumbnails

//Assuming that your thumbnails are all the same size:
const float thumbWidth = 60.f;//Just a random number for example's sake
const float thumbHeight = 90.f;
//Determine how many thumbnails per row are in your matrix.
//I use 320.f as it is the default width of the view, use the width of your view if it is not fullscreen
const int matrixWidth = 320.f / thumbWidth;

const int contentHeight = thumbHeight * (thumbnailImages.count / matrixWidth);

for ( int i = 0; i < thumbnailImages.count; ++i )
{

    int columnIndex = i % matrixWidth;
    int rowIndex = i / matrixWidth;//Intentionally integer math

    UIImage* thumbImage = [thumbnailImages objectAtIndex:i];
    UIButton* thumbButton = [[UIButton alloc] initWithFrame:CGRectMake(columnIndex*thumbWidth, rowIndex*thumbHeight, thumbWidth, thumbHeight)];

    thumbButton.imageView.image = thumbImage;

    [scrollView addSubView:thumbButton];

    [thumbButton release];
}

[scrollView setContentSize: CGSizeMake(320.f, contentHeight)];

Don't necessarily take this word for word as code, I just wrote this up in notepad, but it should give you the general idea of how to do it

