//  AppDelegate.m
// UI2_UICollectionViewPicture
//
// 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
// UI2_UICollectionViewPicture
//
// Created by zhangxueming on 15/7/16.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end //
// ViewController.m
// UI2_UICollectionViewPicture
//
// 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.
//创建数据源
[self createDataList];
//创建UI
[self createUI];
//代理
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
} - (void)createDataList
{
_dataList = [NSMutableArray array];
for (int i=0; i<10; i++) {
NSString *name = [NSString stringWithFormat:@"superCar%i", i];
NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[_dataList addObject:image];
}
} //创建UI - (void)createUI
{
//创建布局对象
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//上下左右边距
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//cell的大小
layout.itemSize = CGSizeMake(150, 100);
//cell横向间距
layout.minimumInteritemSpacing = 20;
//cell纵向间距
layout.minimumLineSpacing = 30;
//创建collectionView
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor whiteColor];
//设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self; //注册cell [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCellReuseId]; [self.view addSubview:_collectionView];
} #pragma mark ---delegate--- //返回分区个数
- (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
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseId forIndexPath:indexPath]; for (UIView *view in cell.contentView.subviews) {
[view removeFromSuperview];
} UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 100)];
imageView.image = _dataList[indexPath.item];
[cell.contentView addSubview:imageView]; return cell;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

UI2_UICollectionViewPicture的更多相关文章

随机推荐

  1. Java对证书的操作

    1 Java程序从证书文件读取证书 import java.io.*; import java.security.cert.*; public class PrintCert{ public stat ...

  2. dsPIC33EP timer1 初始化设置及应用

    //文件 p33timer1.h #ifndef _P33TIMER1_H_ #define _P33TIMER1_H_ //#include "p33timer1.h" #def ...

  3. 关于Android圆形图片的一种优化方案(可以显示网络图片)

    在Android App中,我们经常看到圆形头像图片,然后网上也有很多开源的控件.刚好这个项目用到了,也去找了一些开源的,发现并不完美,所以只好自己优化了,废话不多说,先上效果图: 下面是源码:本人能 ...

  4. javascript实现单例模式

    1.简单实现单例模式: var singleTon = function(){ var _pria = 'private value'; var show_pria = function(){ con ...

  5. MySQL优化---DBA对MySQL优化的一些总结

      非原创, 来自梦嘉朋友, 非常好的总结, 一起学习. ------------------------------------------------- 1. 要确保有足够的内存数据库能够高效的运 ...

  6. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  7. 关于composer

    Composer PHP依赖管理的新时代http://segmentfault.com/a/1190000000353129 Composer 的主页   https://getcomposer.or ...

  8. Apache Shiro 使用手册---转载

    原文地址:http://www.360doc.com/content/12/0104/13/834950_177177202.shtml (一)Shiro架构介绍 一.什么是Shiro Apache ...

  9. IOS 视图切换动画

    我在网上找到的这个小方法,被我举一反三使用的屡试不爽.比如用在,当视图需要执行某一方法跳转到新的一个UIView上,从底层渐变浮到最上层.就是一个不错的视觉效果或者当需要类似keyboard的效果从底 ...

  10. Laravel自学第一课:laravel下载与安装

    本地安装laravel,php环境要配置好,推荐xmapp一键搭建. 1.程序包直接从官方下载,官方开源地址:https://github.com/laravel/laravel(当然也可从此网站:h ...