IOS6-UICollectionViewController

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。