#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = [[RootViewController alloc] init]; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end
#import "RootViewController.h"
#import "ImageViewCell.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define line_gap 10
#define interitem_gap 20
@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
}
@end static NSString *identifier = @"cell";
static NSString *headerIdentifier = @"header";
static NSString *flooterIdentifier = @"floot";
@implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
//创建布局对象
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 设置滚动的方向
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
//行的间隙
layout.minimumLineSpacing = line_gap;
//列的间隙
layout.minimumInteritemSpacing = ;
//item的大小
layout.itemSize = CGSizeMake((SCREEN_WIDTH - *line_gap)/, );
//创建collectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
collectionView.backgroundColor = [UIColor greenColor];
// 设置代理
collectionView.dataSource = self;
collectionView.delegate = self;
//告诉系统将来需要创建什么样的cell(在获取cell之前必须先注册一个cell到系统中)
[collectionView registerClass:[ImageViewCell class] forCellWithReuseIdentifier:identifier];
// 注册头视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];
// 注册尾视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:flooterIdentifier];
[self.view addSubview:collectionView];
}
// 告诉系统一共有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return ;
}
// 告诉系统第section组有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return ;
}
// 告诉系统indexPath的第Section组的item行显示什么内容
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.photo.image = [UIImage imageNamed:@"cellPhoto"];
return cell;
}
//返回头headerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size = CGSizeMake(SCREEN_WIDTH, +line_gap);
return size;
}
//返回头flooterView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
CGSize size = CGSizeMake(SCREEN_WIDTH, +line_gap);
return size;
}
// 头和尾部显示的内容
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView *reusableView = nil;
if (kind == UICollectionElementKindSectionHeader) {
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
UIImageView *headerPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, )];
headerPhoto.image = [UIImage imageNamed:@"headerPhoto"];
[reusableView addSubview:headerPhoto];
}else if (kind == UICollectionElementKindSectionFooter){
reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:flooterIdentifier forIndexPath:indexPath];
UIImageView *flooterPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(, line_gap, SCREEN_WIDTH, )];
flooterPhoto.image = [UIImage imageNamed:@"flooterPhoto"];
[reusableView addSubview:flooterPhoto];
}
return reusableView;
} - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"==%lu",indexPath.row);
} @end
#import <UIKit/UIKit.h>

@interface ImageViewCell : UICollectionViewCell

@property (nonatomic, strong) UIImageView *photo;

@end
#import "ImageViewCell.h"

@implementation ImageViewCell

- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.photo = [[UIImageView alloc] init];
self.photo.frame = self.bounds;
[self addSubview:self.photo];
}
return self;
} @end

iOS UICollectionView之三(基本用法)的更多相关文章

  1. IOS设计模式之三:MVC模式

    IOS设计模式之三:MVC模式   模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...

  2. iOS UICollectionView高级用法(长按自由移动cell)-新

    [reference]http://www.jianshu.com/p/31d07bf32d62 iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. ...

  3. iOS UICollectionView高级用法(长按自由移动cell)

    iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好. @property (nonatomic, strong) UIColle ...

  4. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  5. iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法

    今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...

  6. ios多线程-GCD基本用法

    ios中多线程有三种,NSTread, NSOperation,GCD 这篇就讲讲GCD的基本用法 平时比较多使用和看到的是: dispatch_async(dispatch_get_global_q ...

  7. iOS中block的用法 以及和函数用法的区别

    ios中block的用法和函数的用法大致相同 但是block的用法的灵活性更高: 不带参数的block: void ^(MyBlock)() = ^{}; 调用的时候  MyBlock(); 带参数的 ...

  8. iOS UIProgressView控件用法

    IOS中进度条控件的用法总结. 进度条控件是IOS开发中一个简单的系统控件,使用总结如下: 初始化一个进度条: - (instancetype)initWithProgressViewStyle:(U ...

  9. iOS UICollectionView的实现

    ios的UICollectionView并不能在iOS6之前的版本中使用,为了兼容之前的版本需要自定义UICollectionView.写完之后发现人家已经有开源了,下过来看了看发现我是用UIScro ...

随机推荐

  1. 在archlinux上搭建twitter storm cluster

    本文详细描述如何在archlinux上搭建twitter storm cluster,转载请注明出处,谢谢. 有关archlinux基本系统安装,请参照archlinux简明安装指南一文,下面以上述为 ...

  2. 关于DWZ模板中全选的使用

    只在使用DWZ框架时有用 模板中 <input type="checkbox" name="rule_id[]" />选项1 <input t ...

  3. THinkPHP在模板中的volist循环使用外部变量注意事项

    循环中.自己定义的id=volist就可以使用$volist.id 或者$volist['id'] 而外面定义的变量就不能使用$vo.id 必须使用$vo['id']<volist name=& ...

  4. nor flash和nand flash的区别

    NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面.紧接着,1989年,东芝公司发表了 ...

  5. n0_n1

    #include<stdio.h>int a[10];void quanpailie(int i){ if(i==10)  {for(i=0;i<10;i++)  {   print ...

  6. 【转】Laravel+Angularjs+D3打造可视化数据,RESTful+Ajax

    http://897371388.iteye.com/blog/1975351 大致思路也就是下面,由于最近在学Laravel也在学Angularjs,加上之前做的项目用到了d3. 原来的方案如下: ...

  7. ecshop insert用法

    1 {insert name='ads' id=$ads_id num=$ads_num} 控制语句是在 /includes/lib_insert.php 文件.这个文件是ecshop动态内容函数库. ...

  8. python 之 utf-8编码的秘密

    python3的默认编码方案是utf-8编码,看了些资料,来做总结. 要说utf-8,就要说说unicode,要说unicode,就要说ASCII,我们还是慢慢来. 1.ASCII ASCII编码最初 ...

  9. HIVE中的几种排序

    1.order by:全局排序 select * from emp order by sal; 2.sort by:对于每个reduce进行排序 set mapreduce.job.reduces=3 ...

  10. 【Java 进阶篇】【第二课】异常处理

    概念 异常处理的存在是为了:   允许程序员跳过无法处理的问题,继续开发后续的工作,或根据异常做出更加聪明的处理方式. Java中存在一类对象叫“异常对象”. 当异常情况发生时,就会暗战预先的设定,抛 ...