[IOS UICollectionView模版]

创建CollectionCell模版:

1、新建类CollectionCell继承自UICollectionViewCell

2、新建Xib,命名为CollectionCell.xib

a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116;

 

b.选中刚刚添加的Cell,更改类名为CollectionCell,如图4

c.在CollectionCell.xib的CollectionCell中添加一个ImageView和一个Label(图5)

d.创建映射, 图6,图7

e.选中CollectionCell.m , 重写init方法 

- (id)initWithFrame:(CGRect)frame  
{  
    self = [super initWithFrame:frame];  
    if (self)  
    {  
        // 初始化时加载collectionCell.xib文件  
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];  
          
        // 如果路径不存在,return nil  
        if (arrayOfViews.count < 1)  
        {  
            return nil;  
        }  
        // 如果xib中view不属于UICollectionViewCell类,return nil  
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])  
        {  
            return nil;  
        }  
        // 加载nib  
        self = [arrayOfViews objectAtIndex:0];  
    }  
    return self;  
}  
 

f.选中CollectionCell.xib 修改其identifier为CollectionCell。

 

======================================================

indexviewcontroller.h

#import <UIKit/UIKit.h>
#import "CollectionCell.h"
@interface indexviewcontroller : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@end

indexviewcontroller.m

#import "indexviewcontroller.h"

@interface indexviewcontroller ()

@end

@implementation indexviewcontroller

UICollectionView *cv;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadcollectionview];
}

-(void)loadcollectionview{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setItemSize:CGSizeMake(80, 100)];//设置cell的尺寸
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];//设置其布局方向
    
    cv=[[UICollectionView alloc]initWithFrame:CGRectMake(40,100,240,400) collectionViewLayout:flowLayout];
    
    [cv registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    
    [cv setBackgroundColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.75]];
    cv.delegate=self;
    cv.dataSource=self;
    [self.view addSubview:cv];
}
#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"CollectionCell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor=[UIColor grayColor];
    
    return cell;
}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80, 100);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
posted @ 2014-09-19 11:50  Rayshen  阅读(265)  评论(0编辑  收藏  举报