我在tableview自定义单元格中有不同数量的照片。现在我想显示那些照片 在下一页每行4张照片。我可以这样做吗?
答案 0 :(得分:1)
您可以使用UIScrollView
...
网格中的每个项目都是UIImageView
,您可以将其作为子视图添加到UIScrollView
。
以下代码可以在视图控制器中的viewDidLoad
中进行,并假设您已在界面构建器中设置了UISCrollView,否则您需要在viewDidLoad
内分配滚动视图。
代码将向具有4列的UIScrollView添加可变数量的图像项。
UIImageView *imageView = nil;
//The x and y view location coordinates for your menu items
int x = 0, y = 0;
//The number of images you want
int numOfItemsToAdd = 10;
//The height and width of your images are the screen width devided by the number of columns
int imageHeight = 320/4, imageWidth = 320/4;
int numberOfColumns = 4;
//The content seize needs to refelect the number of items that will be added
[scrollView setContentSize:CGSizeMake(320, imageHeight*numOfItemsToAdd];
for(int i=0; i<numOfItemsToAdd; i++){
if(i%numberOfColumns == 0){//% the number of columns you want
x = 0;
if(i!=0)
y += imageHeight;
}else{
x = imageHeight;
}
imageView = [[UIImageView alloc] initWithFrame: CGRectMake(x, y, imageWidth, imageHeight)];
//set the center of the image in the scrollviews coordinate system
imageView.center = CGPointMake(x, y);
imageView.image = [UIImage imageNamed:@"my_photo.png"];
//Finaly add the image to the scorll view
[scrollView addSubview:imageView];