//  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. Delphi调用WebService(通过SoapHeader认证)经验总结

    项目(Delphi开发)需要调用另一个系统的WebService.走了不少弯路,现记录总结一下经验.以下是WebService要求: 1.WebService概述 营销Webservice接口采用Ap ...

  2. Android---3种方式限制EditView输入字数(转载)

    方法一:利用TextWatcher editText.addTextChangedListener(new TextWatcher() { private CharSequence temp; pri ...

  3. 【JavsScript】转载---如何成为优秀的前端

    题记 做好前端 关于离职 如何成为优秀的前端 书籍推荐 博客推荐 源码阅读 去面试 14年计划 招聘信息 题记 四月前,低迷.失志踌躇不前形容自己再好不过,中途来了一次彻底的醒悟,于是我发现自己变得勤 ...

  4. 各种ORM安装

    1.EF安装 2.PetaPoco安装 Install-Package PetaPoco 3.

  5. 对cocos2d 之autorelease\ratain\release的理解

    前言: 三种情况,引出问题     new出来的对象需要释放,而释放时,如果有其他人引用了这个对象,再次使用这个对象时,则会导致无效指针报错.     于是有了引用计数的施放管理机制.       对 ...

  6. vsftpd虚拟用户创建实例(转载)

    vsftpd虚拟用户创建实例 发布:theboy   来源:net     [大 中 小] vsftpd虚拟用户创建实例,有需要的朋友可以参考下.  vsftpd虚拟用户创建实例,有需要的朋友可以参考 ...

  7. 浅谈 qmake 之 pro、pri、prf、prl文件

    尽管每次和cmake对比起来,我们总是说 qmake 简单.功能少.但是qmake仍然是一个非常复杂的东西,我想大多人应该和我一样吧: 不是太清楚CONFIG等变量到底如何起作用的 用过的qmake内 ...

  8. 【PHP代码审计】 那些年我们一起挖掘SQL注入 - 7.全局防护盲点的总结上篇

    0x01 背景 现在的WEB应用对SQL注入的防护基本都是判断GPC是否开启,然后使用addlashes函数对单引号等特殊字符进行转义.但仅仅使用这样的防护是存在很多盲点的,比如最经典的整型参数传递, ...

  9. iOS 应用有用户评论功能 因为潜在色情信息被退回解决方案

    应用的每一次退回都是一次很好的学习机会 这次是说我的应用没有对色情的内容进行监管或屏蔽的管理机制 这里我主要是 评论页面 违法 如下 以下提供几种解决方案: 1.按照苹果给的建议  我们添加协议进去 ...

  10. Tested work with China Digiprog 3 4.94 mileage programmer

    I was thinking about buying a Digiprog3 clone from China I know that YANHUA Digiprog 3 is the best a ...