UIScrollView 与 UICollectionView
Posted
技术标签:
【中文标题】UIScrollView 与 UICollectionView【英文标题】:UIScrollView With UICollectionView 【发布时间】:2013-03-08 06:35:31 【问题描述】:我有 UIScrollView 和 UICollectionView。在 CollectionView 中有 25 张 100x100 大小的图像。当我运行我的应用程序时,它会在模拟器中显示前 12 个图像,然后我向上滚动,然后它会显示下一个图像。那不是我的问题。我想以水平模式而不是垂直模式显示图像。表示在第一页显示 12 张图像,然后水平滚动并显示下一个图像,而不是垂直显示。请告诉我该怎么做。
谢谢。
#import "Collection.h"
#import "PageViewController.h"
@interface Collection ()
@end
@implementation Collection
@synthesize collView,selectedAlbum,img1;
static NSString *const cellReuseIdentifier = @"CollectionViewCell";
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
// Custom initialization
return self;
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self.collView registerNib:[UINib nibWithNibName:@"CollectionViewItem" bundle:nil] forCellWithReuseIdentifier:cellReuseIdentifier];
self.collView.backgroundColor = [UIColor blackColor];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 100)];
[flowLayout setMinimumLineSpacing:10];
[flowLayout setMinimumInteritemSpacing:10];
// [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
[collView setCollectionViewLayout:flowLayout];
collView.delegate = self;
//Customize scrollView
scrolView = [[UIScrollView alloc] init];
scrolView.frame = CGRectMake(0, 0, 320, 400);
scrolView.delegate = self;
[self.view addSubview:scrolView];
scrolView.contentSize = CGSizeMake(0,0,640,450);
[scrolView addSubView:collView];
Images = [NSArray arrayWithObjects:@"3(3).jpg",@"baby1.jpg",@"Baby2.jpg",@"Baby3.jpg",@"boat_sea_beach-normal.jpg",@"cool.jpg",@"gold-iphone-5-300912.jpg",@"life-sometimes.jpg",@"Mac Wallpapers.jpeg",@"Quotes.jpg",@"series.jpg",@"wallpaper.jpg",@"wide-9.jpg", nil];
self.navigationItem.title = selectedAlbum;
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
return 1;
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
return Images.count;
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell1 = [collView dequeueReusableCellWithReuseIdentifier:cellReuseIdentifier forIndexPath:indexPath];
UIImageView *imageImages = (UIImageView *) [cell1 viewWithTag:16];
imageImages.image = [UIImage imageNamed:[Images objectAtIndex:indexPath.row]];
return cell1;
@end
【问题讨论】:
【参考方案1】:你需要用这个替换你的(注释的)滚动方向:
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
【讨论】:
以上是关于UIScrollView 与 UICollectionView的主要内容,如果未能解决你的问题,请参考以下文章
UIScrollView 与 UIImageView。图像未向上滚动
UIScrollView SetContentSize 与自动布局
如何使 UIButton 与 UIScrollView 一起移动