UICollectionView 学习


//  SonViewController.m

//  testStoryboard



#import "SonViewController.h"

@interface SonViewController ()

@property(nonatomic,strong)UICollectionView *collView;

@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;

@end

@implementation SonViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.



    self.title=@"father";

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemClose target:self action:@selector(popVC)];



    [self.view addSubview:self.collView];

    [self.collView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"itemcell"];

    [self.collView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headercell"];

    [self.collView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footercell"];

}

-(void)popVC{

    [self.navigationController popViewControllerAnimated:YES];

}

- (UICollectionView *)collView{

    if(!_collView) {

        _collView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:self.flowLayout];

        _collView.backgroundColor = [UIColor whiteColor];

        _collView.scrollEnabled=YES;

        _collView.delegate=self;

        _collView.dataSource=self;

    }

    return _collView;

}

- (UICollectionViewFlowLayout *)flowLayout{

    if (!_flowLayout) {

        _flowLayout = [[UICollectionViewFlowLayout alloc] init];

        _flowLayout.minimumInteritemSpacing = 20;//列间距

        _flowLayout.minimumLineSpacing = 20;//列间距

        _flowLayout.itemSize = CGSizeMake(50, 50);//item的大小

        _flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//每个分区的 上左下右 的内边距

        _flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);//区头和区尾的大小

        _flowLayout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);

        _flowLayout.sectionHeadersPinToVisibleBounds = YES;//头视图和尾视图 是否始终固定在屏幕上边和下边

        _flowLayout.sectionFootersPinToVisibleBounds = YES;

        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    }

    return _flowLayout;

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{



    return2;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return12;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{



    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemcell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor systemTealColor];

    returncell;

}

- (void)collectionView:(UICollectionView*)collectionViewdidSelectItemAtIndexPath:(NSIndexPath*)indexPath{



    NSLog(@"select %ld %ld",(long)indexPath.section,(long)indexPath.row);

}

#pragma mark- 头部 尾部

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{



    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

         UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headercell" forIndexPath:indexPath];

           header.backgroundColor = [UIColor systemYellowColor];

           returnheader;

    }

    UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footercell" forIndexPath:indexPath];

    footer.backgroundColor = [UIColor systemGreenColor];

    returnfooter;

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

@end

你可能感兴趣的:(UICollectionView 学习)