UICollectionView 學(xué)習(xí)


//  SonViewController.m

//  testStoryboard



#import "SonViewController.h"

@interface SonViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@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);//每個(gè)分區(qū)的 上左下右 的內(nèi)邊距

        _flowLayout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);//區(qū)頭和區(qū)尾的大小

        _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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容