#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. The P4 Language Specification v1.0.2 Parser

    <p4规范>解析器部分详解 p4解析器是根据有限状态机的思想来设计的. 解析器中解析的过程可以被一个解析图(parser graph)所表示,解析图中所表示的某一个状态(或者说,在P4语言 ...

  2. Nginx 笔记与总结(3)配置虚拟主机

    Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`: /usr/local/nginx/sbin/nginx -s ...

  3. MySQL 数据库设计 笔记与总结(2)逻辑设计

    [实例演示 —— 实体之间的关系] [逻辑设计的工作] ① 将需求转化为数据库的逻辑模型 ② 通过 ER 图的形式对逻辑模型进行展示 ③ 同所选用的具体的 DBMS 系统无关 [名词解释] 候选码可以 ...

  4. Guilty Gear Xrd 资源Rip(1)

    资源破解   首先先要下载GGXrd的PS3游戏,用psarc.exe先把游戏解包 http://files.cnblogs.com/TracePlus/psarc.exe.zip   下载UMode ...

  5. 【IOS笔记】Gesture Recognizers

    Gesture Recognizers Gesture recognizers convert low-level event handling code into higher-level acti ...

  6. 学习VS生活

    很多时候,失败的原因归结为一点:我没有时间...代码敲不完,我真的是没有时间么?很多时候是没意识的浪费时间 我每次进教室,总能看到吴刚和赵东亮在敲代码,为啥他们有时间呢?很多时候,时间就像那啥,挤一挤 ...

  7. Environment Variables

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx Every process has an ...

  8. ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

    https://en.wikipedia.org/wiki/Base64 The Base64 index table: Value Char   Value Char   Value Char   ...

  9. Peeking into Apache Flink's Engine Room

    http://flink.apache.org/news/2015/03/13/peeking-into-Apache-Flinks-Engine-Room.html   Join Processin ...

  10. phpexcel 读取数据

    最近公司做一个客户导入会员的功能,以前导入都是使用csv格式导入的,但是客户反应问题挺多的,普遍是乱码(由于各种系统各种环境可能引起编码问题).最近想着就把这个导入完全改成excel导入,就研究了下p ...