//  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. Objective-C的对象模型和runtime机制

    内容列表 对象模型(结构定义,类对象.元类和实例对象的关系) 消息传递和转发机制 runtime系统功能理解 对象模型 结构定义 对象(Object): OC中基本构造单元 (building blo ...

  2. JAVA正则表达式语法大全

    [正则表达式]文本框输入内容控制 整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n ...

  3. 基于W5500的嵌入式SNMP代理端实现

     一 实验背景 近期一个做焊接设备的朋友想在焊机上加入监控的新功能,实时获取焊机的温度.功耗等參数,还可简单控制,实现对集群焊接设备的网络化管理.而这个朋友不想在开发管理系统上花太多精力,想找一个 ...

  4. stm32出现错误“identifier file is undefined”

    为什么记录这个问题,说来很简单,这已经是第二次犯这个小错误了. 出现了错误“identifier file is undefined”的解决方法;option->general options- ...

  5. Myeclipse2014添加mybatis generator插件

    Myeclipse2014把mybatis generator插件直接放在dropins文件夹下,重启后不能成功安装mybatis插件. 既然离线安装不成功,可以选择在线安装 1.选择 Help-&g ...

  6. sharepreferce支持boolean,string类型

    public class SharePrefersUtils { private static final String name="cogi"; public static bo ...

  7. 【Mood-18】github 使用指南

    windows下使用教程: http://www.cnblogs.com/dongdong230/p/4211221.html repository not found error问题解决(需确定gi ...

  8. Umbraco(4)-Outputting the Document Type Properties(翻译文档)

    翻译原文地址:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/umbraco4outputting-the-document-typ ...

  9. ubuntu下规避终端打开gvim出现的错误

    在终端下面打开gvim会出现下面的错误: GLib-GObject-WARNING **: Attempt to add property GnomeProgram::display after cl ...

  10. hdu 4411 最小费用流

    思路:主要就是要把一个每个城市拆为两个点,建一条容量为1,费用为-inf的边,保证每个城市都会被遍历. /*最小费用最大流*/ #include<iostream> #include< ...