我对集合视图是全新的,请在集合视图中帮助我垂直和水平滚动单元格:
viewcontroller.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *collectionView;
}
@end
viewcontroller.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionView=[[UICollectionView alloc] initWithFrame:self.view.frame
collectionViewLayout:layout];
[collectionView setDataSource:self];
[collectionView setDelegate:self];
[collectionView registerClass:[UICollectionViewCell class]
forCellWithReuseIdentifier:@"cellIdentifier"];
[collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:collectionView];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 9;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(100, 100);
}
@end
答案 0 :(得分:1)
我在这里有点晚了,但是为了防止其他人查看,你可以使用UICollectionViewFlowLayout轻松设置你的集合视图滚动方向。
飘柔。
- (instancetype)init { UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(106,106); layout.minimumInteritemSpacing = 1; layout.minimumLineSpacing = 1; layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self = [super initWithCollectionViewLayout:layout]; 回归自我; }
答案 1 :(得分:0)
您可以使用以下UICollectionView类的方法以编程方式滚动集合视图:
[collectionView scrollsToTop];
[collectionView scrollToItemAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UICollectionViewScrollPosition)#> animated:<#(BOOL)#>];
[collectionView scrollRectToVisible:<#(CGRect)#> animated:<#(BOOL)#>];