码迷,mamicode.com
首页 > 移动开发 > 详细

[IOS UICollectionView模版]

时间:2014-09-19 13:23:45      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   文件   

创建CollectionCell模版:

1、新建类CollectionCell继承自UICollectionViewCell

bubuko.com,布布扣

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

bubuko.com,布布扣

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

bubuko.com,布布扣 

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

bubuko.com,布布扣

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

bubuko.com,布布扣

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

bubuko.com,布布扣

bubuko.com,布布扣

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

[IOS UICollectionView模版]

标签:style   blog   http   color   io   os   ar   for   文件   

原文地址:http://www.cnblogs.com/rayshen/p/3981136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!