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

时间: 2024-08-07 08:38:08

IOS6-UICollectionViewController的相关文章

UITableView Group类型扁平化适配iOS6.0

适配方案 tableView // 在ViewDidLoad里调用该方法 - (void)unifyTableView:(UITableView *)tableView { //iOS7以后将分隔线拉至边线 if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) { [tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([self.tableView responds

iOS6之后 NSAttributedString 福利

@于iOS6之前,需要使用NSMutableAttributedString当你需要导入:CoreText.framework框架的.但在iOS6 之后就不在须要了. - (void)testOfNSMutableAttributedStringAndNSAttributedString { /** * - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; * 主要方法 * name 属性名 * v

iOS6 及其以上版本自动旋转、手动强制旋转方案及布局适配

1.布局适配方式 本文不讨论哪种布局适配方式最好,此处使用的是 Masonry 纯代码布局适配.(Masonry 底层就是 AutoLayout 的 NSLayoutConstraint) 2.iOS 方向枚举类 // 三维设备方向 typedef NS_ENUM(NSInteger, UIDeviceOrientation) { UIDeviceOrientationUnknown, UIDeviceOrientationPortrait, // Device oriented vertica

IOS6.0自带下拉刷新控件UIRefreshControl

1.UIRefreshControl必须要在IOS6.0以后才能使用,同时他只能在UITableViewController类中才可以使用 2.使用比较简单 self.refreshControl = [[UIRefreshControl alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 100)]; [self.refreshControl addTarget:self action:@selector(

iOS6 自适应旋转处理

一.iOS6之后UIViewController 旋转处理 1.view controller 是否支持自动旋转 - (BOOL)shouldAutorotate; 2.view controller 支持哪几种 - (NSUInteger)supportedInterfaceOrientations; 在UIApplication中定义了屏幕方向相关的枚举 UIInterfaceOrientationMask枚举类型 定义了支持的屏幕类型 UIInterfaceOrientation枚举类型

iOS6之后 NSAttributedString 的福利

@在iOS6之前需要使用NSMutableAttributedString时都需要导入:CoreText.framework框架的,但在iOS6 之后就不在需要了. - (void)testOfNSMutableAttributedStringAndNSAttributedString { /** * - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; * 主要方法 * name 属性名 * va

iOS6的旋屏控制技巧

iOS6的旋屏控制技巧 在iOS5.1 和 之前的版本中, 我们通常利用 shouldAutorotateToInterfaceOrientation: 来单独控制某个UIViewController的旋屏方向支持,比如: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceO

iOS6、7、8、9新特性总汇和适配说明

转自:http://blog.6ag.cn/1195.html iOS6新特性 一.关于内存警告 ios6中废除了viewDidUnload,viewWillUnload这两个系统回调,收到内存警告时在didReceiveMemoryWarning中进行相关的处理. Crayon Syntax Highlighter v2.7.1 - (void)viewDidUnload { [super viewDidUnload]; // 处理 ios6 以下的系统内存警告系统回调消息 } // 这里处理

IOS之UICollectionViewController ------------ 救赎之路

UICollectionViewColltroller是IOS中用来展示数据集的视图,功能强大,而且能实现的视觉丰富,这篇随笔笔者就演示下UICollectionViewConlltroller简单的使用,献给同样在编程之路前进的人. 首先来看看UIColletionViewController各种效果(素材来源百度) UICollectionViewController 有三个非常重要的协议UICollectionViewDelegate,UICollectionViewDataSource,

跳转UICollectionViewController报Could not load NIB in bundle解决办法

报错代码如下:'Could not load NIB in bundle: 'NSBundle </Users/mac/Library/Developer/CoreSimulator/Devices/A0182119-C6AC-4732-BF37-3E6D35B102E1/data/Containers/Bundle/Application/E5F586ED-526F-4D52-9C7C-9335E772F666/00-ItcastLottery.app> (loaded)' with nam