自定義UICollectionView

1.建立一個UICollectionView工程,點擊鼠標右側按鈕選擇New File->Cocoa Class->點擊Next,Class選項填寫一個合理的名稱,如:MyCollectionViewCell,而後點擊Next。app

 

2.AppDelegate.m文件中導入頭文件「#import 「ViewController.h」」,而後填寫以下代碼:ide

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {atom

    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];orm

    self.window.rootViewController=nav;blog

    return YES;ci

}rem

 

 3.ViewController.m文件代碼string

#import "ViewController.h"it

#import "MyCollectionViewCell.h"io

#import "Header.h"

 

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>{

    UICollectionView  *mainCollectionView;

}

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    self.navigationController.navigationBar.translucent=NO;

    self.navigationController.navigationBar.barTintColor=[UIColor purpleColor];

    

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];

    //設置headerView的尺寸大小

    layout.headerReferenceSize = CGSizeMake(WIDTH, 0);

    //該方法也能夠設置itemSize

    layout.itemSize =CGSizeMake(90, 150);

    

    mainCollectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];//初始化

    //註冊UICollectionViewCell

    [mainCollectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];

    mainCollectionView.dataSource=self;

    mainCollectionView.delegate=self;

    mainCollectionView.backgroundColor=[UIColor whiteColor];

    [self.view addSubview:mainCollectionView];

}

 

 

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

    return 3;

}

 

 

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

    return 9;

}

 

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

    static NSString *identifier=@"cell";

    MyCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    cell.nameLable.text=[NSString stringWithFormat:@"{%ld,%ld}",(long)indexPath.section,(long)indexPath.row];

    cell.imageView.image=[UIImage imageNamed:@"photo"];

    cell.backgroundColor=[UIColor yellowColor];

    return cell;

}

 

//設置每一個item的尺寸

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

//    return CGSizeMake(90, 130);

//}

 

//設置每一個item的UIEdgeInsets

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10, 10, 10, 10);

}

 

//若是一組中有多行item,設置行間距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{

    return 10;

}

 

//設置兩個組之間的列間距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{

    return 15;

}

 

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

    //width的設置對該方法無影響

    return CGSizeMake(300, 30);

}

 

//經過設置SupplementaryViewOfKind 來設置頭部或者底部的view,其中 ReuseIdentifier 的值必須和 註冊是填寫的一致,本例都爲 「reusableView」

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

{

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];

    headerView.backgroundColor =[UIColor grayColor];

    //解決重用機制的bug

    for (UIView *view in headerView.subviews) {

        [view removeFromSuperview];

    }

    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];

    if (indexPath.section==0) {

        label.text = @"食品類";

    }

    if (indexPath.section==1) {

        label.text = @"水果類";

    }

    if (indexPath.section==2) {

        label.text = @"家用類";

    }

    label.font = [UIFont systemFontOfSize:20];

    [headerView addSubview:label];

    return headerView;

}

@end

 

4.MyCollectionView.h文件代碼

#import <UIKit/UIKit.h>

@interface MyCollectionViewCell : UICollectionViewCell

@property(nonatomic,strong)UIImageView *imageView;

@property(nonatomic,strong)UILabel     *nameLable;

@end

 

5.MyCollectionView.m文件代碼

#import "MyCollectionViewCell.h"

 

@implementation MyCollectionViewCell

 

-(instancetype)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];

    if (self) {

        _imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 70)];

        [self addSubview:_imageView];

        

        _nameLable=[[UILabel alloc]initWithFrame:CGRectMake(10, 80, 70, 30)];

        _nameLable.textAlignment=NSTextAlignmentCenter;

        _nameLable.textColor=[UIColor blueColor];

        _nameLable.font=[UIFont systemFontOfSize:16];

        _nameLable.backgroundColor=[UIColor grayColor];

        [self addSubview:_nameLable];

 

    }

    return self;

}

@end

 

6.效果圖以下:

 

相關文章
相關標籤/搜索