//  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. 如何实现一个c/s模式的flv视频点播系统

    一.写在前面 视频点播,是一个曾经很热,现如今依然很热的一项视频服务技术.本人最近致力于研究将各种视频格式应用于点播系统中,现已研究成功FLV, F4V, MP4, TS格式的视频点播解决方案,完全支 ...

  2. Android 动画机制与使用技巧

    动画效果一直是人机交互中非常重要的部分,与死板.突兀的显示效果不同,动画效果的加入,让交互变得更加友好,特别是在提示.引导类的场景中,合理地使用动画能让用户获得更加愉悦的使用体验 一.Android ...

  3. Android中的Handler的具体用法

    Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行.Android利用Handler来实现UI线程的更新的. Handler是Android中的消息发送器,其在哪个Activit ...

  4. 怎样通过Java程序提交yarn的mapreduce计算任务

    因为项目需求,须要通过Java程序提交Yarn的MapReduce的计算任务.与一般的通过Jar包提交MapReduce任务不同,通过程序提交MapReduce任务须要有点小变动.详见下面代码. 下面 ...

  5. poj 2688 状态压缩dp解tsp

    题意: 裸的tsp. 分析: 用bfs求出随意两点之间的距离后能够暴搜也能够用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发訪问过s集合中的点.眼下在点u走过 ...

  6. php与mysql的链接到底用mysql 还是mysqli,pdo

    参考:http://php.net/manual/en/mysqlinfo.api.choosing.php

  7. 3.3html学习笔记之链接

    iframe 元素会创建包含另外一个文档的内联框架 <iframe src=""/> 跳转链接 <a href="#here" target= ...

  8. JSON特殊字符处理

    JSON 是适用于 Ajax 应用程序的一种有效格式,原因是它使 JavaScript 对象和字符串值之间得以快速转换.由于 Ajax 应用程序非常适合将纯文本发送给服务器端程序并对应地接收纯文本,相 ...

  9. ScrollView嵌套ListView的滑动冲突问题,是看大神的方法的,作为学习以后用的到

    在工作中,曾多次碰到ScrollView嵌套ListView的问题,网上的解决方法有很多种,但是杂而不全.我试过很多种方法,它们各有利弊. 在这里我将会从使用ScrollView嵌套ListView结 ...

  10. IOS 弹出菜单的动态效果

    效果1.点击按钮上浮 2.点击按钮下沉 3.点击按钮下拉展示 4.点击按钮向上收缩 5.左右如是说 关键是改变视图的大小位置的时机正确与否 eg1.1.点击按钮下沉消失 已知myView.frame= ...