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

IOS6-UICollectionViewController

时间:2014-09-07 23:41:15      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   div   

CollectViewController.h
@interface CollectViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataSource;
@property(nonatomic,retain)UICollectionView *myCollectionView;//类似于UITableView
@end
CollectViewController.m
#import "CollectViewController.h"
#import "CollectCell.h"
#import "CollectLayout.h"
@interface CollectViewController ()

@end

@implementation CollectViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImageView *bgImageVeiw = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sharebg"]];
    
    
    CollectLayout *rac = [[CollectLayout alloc]init];
    
    self.myCollectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:rac];//根据位置大小和collectView布局初始化
    [self.myCollectionView registerClass:[CollectCell class] forCellWithReuseIdentifier:@"customCell"];//设置cell的类型
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.view addSubview:self.myCollectionView];
    self.myCollectionView.backgroundView = bgImageVeiw;
    
    self.dataSource = [NSMutableArray arrayWithCapacity:30];
    for (int i = 1; i <= 20; i++)
    {
        NSDictionary *dic = @{@"imageName":[NSString stringWithFormat:@"%d.jpg",i],@"titleName":[NSString stringWithFormat:@"%d",i]};
        [self.dataSource addObject:dic];
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataSource.count/2;//有2个section
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;//每个section有2列
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectCell *collectionCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"customCell" forIndexPath:indexPath];
    if (!collectionCell)
    {
        return nil;
    }
    NSString *imageName = [[self.dataSource objectAtIndex:(indexPath.section*2+indexPath.row)] objectForKey:@"imageName"];
    NSString *titleName = [[self.dataSource objectAtIndex:(indexPath.section*2+indexPath.row)] objectForKey:@"titleName"];
    collectionCell.collectImageView.image = [UIImage imageNamed:imageName];
    collectionCell.collectContent.text = titleName;
    return collectionCell;
}

@end
CollectLayout.m(作用:对CollectionView布局)
#import "CollectLayout.h"

@implementation CollectLayout

-(id)init
{
    self = [super init];
    if (self) {
        self.itemSize = CGSizeMake(150, 150);
//        self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.sectionInset = UIEdgeInsetsMake(20.0, 0.0, 0.0, 0.0);
        self.minimumLineSpacing = 50.0;
        self.minimumInteritemSpacing = 0.0;
    }
    return self;
}

@end
CollectCell.h(类似于UITableCell)
@interface CollectCell : UICollectionViewCell

@property(nonatomic,retain)UIImageView *collectImageView;
@property(nonatomic,retain)UILabel *collectContent;

@end
CollectCell.m
#import "CollectCell.h"

@implementation CollectCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame = CGRectMake(0, 0, 150, 150);
        UIImageView *bgImageView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BookShelfCell"]];
        bgImageView.frame = CGRectMake(0, 0,150,150);
        [self.contentView addSubview:bgImageView];
        
        self.collectImageView = [[UIImageView alloc]initWithFrame:CGRectMake(25,10, 100, 100)];
        [self.contentView addSubview:self.collectImageView];
        
        self.collectContent = [[UILabel alloc]initWithFrame:CGRectMake(0, 110,150,30)];
        self.collectContent.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.collectContent];
    }
    return self;
}

@end

 Demo下载:https://github.com/forrHuen/CollectViewControllerDemo

IOS6-UICollectionViewController

标签:style   blog   http   color   os   io   ar   for   div   

原文地址:http://www.cnblogs.com/huen/p/3948139.html

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