iOS创建UIButtons矩阵

iOS创建UIButtons矩阵,ios,matrix,uibutton,scrollview,Ios,Matrix,Uibutton,Scrollview,我正在从服务器加载预览图像(缩略图),并将其保存到本地文档目录。对于每个图像,都有一个核心数据条目 现在我需要将这个缩略图渲染到一个滚动视图中。我不知道将来会有多少个缩略图,为什么我需要以编程方式将缩略图渲染到ScrollView中。我知道我必须根据缩略图的数量来设置scrollView的高度 缩略图需要可触摸,因为轻触缩略图会打开另一个对话框 第一个问题:用于显示缩略图的正确控件是什么?是否将UIButton设置为自定义按钮,并将缩略图设置为背景图像 第二个问题:如何设置一个动态矩阵来渲染拇指

我正在从服务器加载预览图像(缩略图),并将其保存到本地文档目录。对于每个图像,都有一个核心数据条目

现在我需要将这个缩略图渲染到一个滚动视图中。我不知道将来会有多少个缩略图,为什么我需要以编程方式将缩略图渲染到ScrollView中。我知道我必须根据缩略图的数量来设置scrollView的高度

缩略图需要可触摸,因为轻触缩略图会打开另一个对话框

第一个问题:用于显示缩略图的正确控件是什么?是否将UIButton设置为自定义按钮,并将缩略图设置为背景图像

第二个问题:如何设置一个动态矩阵来渲染拇指(按钮)


谢谢

第一个答案:我建议为此使用
ui按钮
,在这种情况下我会这么做

第二个答案:假设您有一个包含所有缩略图的数组,那么您可以简单地对它们进行迭代,并以如下方式创建所有按钮:

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)];
NSArray*缩略图图像;
UIScrollView*滚动视图;
//Scrollview通过IB链接或动态创建…任何对您来说更容易的
//缩略图图像数组由缩略图列表填充
//假设您的缩略图大小相同:
常数浮点指宽=60.f//举个例子,只是一个随机数
恒浮指高度=90.f;
//确定矩阵中每行有多少个缩略图。
//我使用320.f,因为它是视图的默认宽度,如果不是全屏,则使用视图的宽度
常数int matrixWidth=320.f/指宽;
const int contentHeight=thumbHeight*(thumbnailImages.count/matrixWidth);
对于(int i=0;i

不必逐字逐句地把它当作代码,我只是把它写在记事本上,但它应该能让你大致了解如何做

好的,这很有效。但是我发现了一个要求,那就是使用UIButton是不可能的。如果用户在缩略图上点击并按住手指较长时间,则用户应该可以删除缩略图以及底层核心数据和documents目录中的数据。有什么想法吗?嗯,我不确定。我不知道这些事情在iOS中是如何正常处理的,如果有某种手势识别器的话。你应该在这里查看UIButton类参考:看看是否有你需要的事件或选择器。你也可以创建自己的自定义视图,使用UIImageView,或者以某种方式对UIButton进行子类化,但是如何设置矩阵的总体思路是我上面概述的。子类化UIButton完成了任务!