我有大约 50 张 50 x 50 像素大小的图像。我希望用户选择其中之一。所以首先我想到了一个 UITableView,但这不是正确的事情。它浪费了大量的屏幕空间。与其将所有图像放在一个下方,不如显示一个网格,比如说 6 列和 n 行。
我会使用 UIScrollView 并用我自动排列的 UIView 对象填充它,使它们看起来像一个网格。这是要走的路吗?或者有什么其他建议?
我有大约 50 张 50 x 50 像素大小的图像。我希望用户选择其中之一。所以首先我想到了一个 UITableView,但这不是正确的事情。它浪费了大量的屏幕空间。与其将所有图像放在一个下方,不如显示一个网格,比如说 6 列和 n 行。
我会使用 UIScrollView 并用我自动排列的 UIView 对象填充它,使它们看起来像一个网格。这是要走的路吗?或者有什么其他建议?
我在我的应用程序中做了一些非常相似的事情——一个 NxN 网格,下面有一个图像,另一个子视图在上面绘制“线条”,所有这些都归 UIScrollView 所有。我建议使用单独的视图来绘制图像,例如:
-(void) drawRect(CGRect rect) {
CGRect smallerRect = CGRectMake(x, y, width, height);
[yourImage drawRect: smallerRect];
// repeat as needed to draw the grid
}
另一张海报提到,如果您的视图归 UIScrollView 所有,您将无法获得触摸事件——这根本不是真的。我有它的工作。您可能需要设置以下内容:
[yourScrollView setUserInteractionEnabled: YES]
[yourGridView setUserInteractionEnabled: YES]
正如您所建议的,表格视图并不一定意味着每行显示一个图像。可以自定义表格单元格,并且具有六个 50x50 UIImageViews 的单元格子类将非常简单。它可能不如滚动视图灵活,但如果每行六张图像是您的目标,那么表格视图是获得它的最快方法。
three20库有一个类可以做到这一点。
three20 太可怕了,我用过,我不推荐它...显示网格很容易...
- (void) reloadGridView
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(5, 54, scrollViewWidth, scrollViewHeight8)];
scrollView.delegate = self;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.userInteractionEnabled = YES;
scrollView.scrollEnabled = YES;
[self.view addSubview:scrollView];
int x = 10;
int y = 10;
divisor = 1;
for (int i = 0; i < [self.photosArray count]; i++) {
int buttonTag = divisor;
UIImage *thumb = [UIImage imageWithContentsOfFile:[self thumbPathAtIndex:i]];
//********* CREATE A BUTTON HERE **********
//********* use the thumb as its backgroundImage *******
if(divisor%4==0){
y+=70;
x = 10;
}else{
x += 70;
}
divisor++;
}
[scrollView setContentSize:CGSizeMake(scrollViewWidth, ([self.photosArray count]%4 == 0) ? y : y+100)];
}
如果您在横向时想要 6 张图像 - 为 isLandscape 设置一个 BOOL
if (isLandscape) {
//change the divisor to change @ 6 instead of 4
}
如果您需要更高级的 gridView,请查看 AQGridView(谷歌)