//  AppDelegate.m
// UI1_UICollectionView
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *root = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
self.window.backgroundColor = [UIColor whiteColor]; return YES;
}
//  ViewController.h
// UI1_UICollectionView
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end //
// ViewController.m
// UI1_UICollectionView
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import "ViewController.h" //重用标志符
#define kCellReuseId @"cellId" @interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *_collectionView;
NSMutableArray *_dataList;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.创建数据源
[self createDataList];
//2.创建UI
[self createCollectionView];
//3.遵守协议,设置代理
} //创建数据源
- (void)createDataList
{
_dataList = [NSMutableArray array];
for (int i=0; i<20; i++) {
NSString *str = [NSString stringWithFormat:@"第%d个网格", i+1];
[_dataList addObject:str];
}
} //创建UI - (void)createCollectionView
{
//第一个参数: collectionView 的位置
//第二个参数: 布局对象, UICollectionViewLayout类(子类)的对象
//规则布局
//UICollectionViewFlowLayout:UICollectionViewLayout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//上下左右边界的距离top, left, bottom, right
layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
//设置cell的大小
layout.itemSize = CGSizeMake(180, 100); //设置横向的最小距离
layout.minimumInteritemSpacing = 5;
//设置竖向的最小距离
layout.minimumLineSpacing = 10; _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout]; //设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self; //注册cell
//第一个参数:cell的类型
//第二个参数:cell的重用标识符
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellReuseId];
_collectionView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:_collectionView];
} #pragma mark ---collectionView代理--- //返回分区的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
} //返回每个分区有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return _dataList.count;
} //返回cell - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//UITableView : indexPath --> section row
//UICollectionView: indexPath --> section item
//从重用队列中取出cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseId forIndexPath:indexPath]; cell.backgroundColor = [UIColor yellowColor]; //移除cell.contentView 的子视图
for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
} //在cell上显示内容
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 180, 40)];
label.text = _dataList[indexPath.item];
label.textAlignment = NSTextAlignmentCenter;
[cell.contentView addSubview:label]; return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UI1_UICollectionView的更多相关文章

随机推荐

  1. Android程序开发0基础教程(一)

    程序猿学英语就上视觉英语网 Android程序开发0基础教程(一)   平台简单介绍   令人激动的Google手机操作系统平台-Android在2007年11月13日正式公布了,这是一个开放源码的操 ...

  2. FindMe

    https://github.com/hongdong/FindMe_Android https://github.com/hongdong/FindMe_Server https://github. ...

  3. [AngularJS] Accessing Services from Console

    Using the Chrome console, you can access your AngularJS injectable services. This is down and dirty ...

  4. iOS开发——图层OC篇&UIColor深入研究(CGColor,CIColor)

    UIColor深入研究(CGColor,CIColor) 由于跟人比较喜欢研究关于图层与动画方面的技术,正打算看看别人写的好东西,就遇到了好几个问题, 第一:UIClor类方法的使用 就是关于UICo ...

  5. Browser 與 Server 持續同步的作法介紹 (Polling, Comet, Long Polling, WebSocket)长连接

    對 Comet 的懵懂 記得兩年多前,第一次看到 Gmail 中的 GTalk 覺得很好奇:「咦?線上聊天且是 Google 的熱門系統,只用傳統的 AJAX 應該會操爆伺服器吧?」很幸運的,當時前公 ...

  6. 自增锁预分配ID

    http://www.cnblogs.com/xpchild/p/3825309.html mysql> show create table pp; CREATE TABLE `pp` ( `i ...

  7. iOS开发,hook系统Objective-C的函数

    我们都知道在windows下可以通过API轻松的hook很多消息,IOS也可以实现hook的功能. 建立一个 TestHookObject类 // // TestHookObject.m // Tes ...

  8. 代码片段 - JavaScript 字符串模板

    /* ------------------------------ // 字符串模板1,语法严格,不能混用,效率相对较高 // 使用 {{ }} 作为标记是为了允许在模板中使用 JSON 字符串 // ...

  9. asp.net小结

    ASP.net是是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,主要用于WEB开发,与我们以前接触CS开发相比,虽然相似点不少,但不同点也是有很多的,我来简单的总结一下. 一.控件 A ...

  10. SQL Server表的数据量大小查询

    今天想在服务器上还原一个DB,发现磁盘空间不够,查看发现,其中一个DB竟然有56G了.因此想收缩一下这个DB,发现大小没多大变化.然后在网上找了找SQL脚本,看能不能查看下哪个表的数据量那么大. 网上 ...