转载自:   http://www.it165.net/pro/html/201312/8575.html

    • Decoration View是UICollectionView的装饰视图。苹果官方给的案例都没涉及到这个视图的使用。没有具体的细节。我今天用UICollectionView做了一个简易的书架。主要是Decoration View的使用方法。

      效果如下:

      基本的UICollectionView使用方法请自己查询。

      #import "CVViewController.h"

      #import "CVCell.h"

      #import "CVLayout.h"

      @interfaceCVViewController ()

      @end

      @implementation CVViewController

      - (void)viewDidLoad

      {

      [superviewDidLoad];

      [self.coll registerClass:[CVCell class] forCellWithReuseIdentifier:@"cell"];

      CVLayout *layout=[[CVLayout alloc] init];

      [self.coll setCollectionViewLayout:layout];

      }

      - (void)didReceiveMemoryWarning

      {

      [superdidReceiveMemoryWarning];

      }

      -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

      return3;

      }

      -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

      return3;

      }

      -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

      UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"forIndexPath:indexPath];

      return cell;

      }

      @end

      其中CVCell是我自定义的一个

      UICollectionViewCell

      其中CVLayout是我自定义的一个

      UICollectionViewLayout

      接下来主要看一下自定义的layout

      #import "CVLayout.h"

      #import "CVDEView.h"

      @implementation CVLayout

      -(void)prepareLayout{

      [super prepareLayout];

      [self registerClass:[CVDEView class] forDecorationViewOfKind:@"CDV"];//注册Decoration View

      }

      -(CGSize)collectionViewContentSize{

      return self.collectionView.frame.size;

      }

      - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path

      {

      UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributeslayoutAttributesForCellWithIndexPath:path];

      attributes.size = CGSizeMake(215/3.0, 303/3.0);

      attributes.center=CGPointMake(80*(path.item+1), 62.5+125*path.section);

      return attributes;

      }

      //Decoration View的布局。

      - (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath *)indexPath{

      UICollectionViewLayoutAttributes* att = [UICollectionViewLayoutAttributeslayoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];

      att.frame=CGRectMake(0, (125*indexPath.section)/2.0, 320, 125);

      att.zIndex=-1;

      return att;

      }

      - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

      NSMutableArray* attributes = [NSMutableArrayarray];

      //把Decoration View的布局加入可见区域布局。

      for (int y=0; y<3; y++) {

      NSIndexPath* indexPath = [NSIndexPathindexPathForItem:3inSection:y];

      [attributes addObject:[selflayoutAttributesForDecorationViewOfKind:@"CDV"atIndexPath:indexPath]];

      }

      for (NSInteger i=0 ; i < 3; i++) {

      for (NSInteger t=0; t<3; t++) {

      NSIndexPath* indexPath = [NSIndexPathindexPathForItem:t inSection:i];

      [attributes addObject:[selflayoutAttributesForItemAtIndexPath:indexPath]];

      }

      }

      return attributes;

      }

      下面是最后的Decoration View的设计。

      首先要继承

      UICollectionReusableView

      ?然后

      @implementation CVDEView

      - (id)initWithFrame:(CGRect)frame

      {

      self = [superinitWithFrame:frame];

      if (self) {

      UIImageView *imageView=[[UIImageViewalloc] initWithFrame:frame];

      imageView.image=[UIImageimageNamed:@"BookShelfCell.png"];

      [selfaddSubview:imageView];

      }

      returnself;

      }

      OK。就可以看到上面图上的效果了。

IOS中集合视图UICollectionView中DecorationView的简易使用方法的更多相关文章

  1. iOS:集合视图UICollectionView、集合视图控制器UICollectionViewController、集合视图单元格UICollectionViewCell(创建表格的另一种控件)

    两种创建表格方式的比较:表格视图.集合视图(二者十分类似) <1>相同点:   表格视图:UITableView(位于storyboard中,通过UIViewController控制器实现 ...

  2. 集合视图UICollectionView 介绍及其示例程序

    UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可 能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的 ...

  3. django变量使用-在模板中使用视图函数中的变量

    DTL语言,即django template language 第一次使用时,需要修改项目的setting.py文件,将其中TEMPLATES中的DIRS修改为os.path.join(BASE_DI ...

  4. 集合视图 UICollectionView

    什么是UICollectionView UICollectionView是一种新的数据展示方式,简单来说可以把他理解成多列的UITableView(请一定注意这是UICollectionView的最最 ...

  5. swift:创建集合视图UICollectionView

    swift中创建集合视图和OC中差不多,主要是实现UICollectionViewDataSource数据源协议和UICollectionViewDelegateFlowLayout自定义布局协议,其 ...

  6. web项目中,视图层中关于相对路径和绝对路径

    1.在jfinal项目中 因为一直使用的jfinal,没感觉路径问题. 举个栗子,项目名字叫做test.访问一个Controller的映射为/user/add.这样,在浏览器地址栏直接:localho ...

  7. IOS中截屏的实现,很简易的方法

    // 添加QuartzCore.framework库 #import <QuartzCore/QuartzCore.h> -(void) screenShot { // 截屏 UIGrap ...

  8. SQL Server 中系统视图sysobjects中type字段的说明

    对象类型: AF = 聚合函数 (CLR) C = CHECK 约束 D = DEFAULT(约束或独立) F = FOREIGN KEY 约束 FN = SQL 标量函数 FS = 程序集 (CLR ...

  9. UICollectionView集合视图的概念

    如何创建UICollectionView 集合视图的布局UICollectionViewFlowLayout 自定义cell 布局协议UICollectionViewDelegateFlowLayou ...

随机推荐

  1. poi 合并单元格、设置边框

    HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); //创建一个样式 HSSFCellStyle sty ...

  2. 以图搜图(一):Python实现dHash算法(转)

    近期研究了一下以图搜图这个炫酷的东西.百度和谷歌都有提供以图搜图的功能,有兴趣可以找一下.当然,不是很深入.深入的话,得运用到深度学习这货.Python深度学习当然不在话下. 这个功能最核心的东西就是 ...

  3. HDU 4828 Grids(卡特兰数+乘法逆元)

    首先我按着我的理解说一下它为什么是卡特兰数,首先卡特兰数有一个很典型的应用就是求1~N个自然数出栈情况的种类数.而这里正好就对应了这种情况.我们要满足题目中给的条件,数字应该是从小到大放置的,1肯定在 ...

  4. HDU 4635 Strongly connected(强连通分量缩点+数学思想)

    题意:给出一个图,如果这个图一开始就不是强连通图,求出最多加多少条边使这个图还能保持非强连通图的性质. 思路:不难想到缩点转化为完全图,然后找把它变成非强连通图需要去掉多少条边,但是应该怎么处理呢…… ...

  5. Mesos架构简介

    1. 前言 同其他大部分分布式系统一样,Apache Mesos为了简化设计,也是采用了master/slave结构,为了解决master单点故障,将master做得尽可能地轻量级,其上面所有的元数据 ...

  6. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  7. C++:new 和 delete

    在 C++ 中 , 使用 new 操作符动态申请内存的时候,如果申请失败,则会抛出  bad_alloc异常 当使用  delete 释放一块内存的时候 , 有些编译器上delete 不能判断一块内存 ...

  8. Linux 监控文件事件

    某些应用程序需要对文件或者目录进行监控,来侦测其是否发生了某些事件.Linux很贴心的为我们提供了inotify API,也是Linux的专有. inotify API 在使用之前一定要有一个inot ...

  9. springMVC拦截器简单配置

    <!-- 拦截器 -->    <mvc:interceptors>        <mvc:interceptor>            <!-- 拦截所 ...

  10. 使用JavaCV/OpenCV抓取并存储摄像头图像

    http://blog.csdn.net/ljsspace/article/details/6702178  分类: 图形图像(3)  版权声明:本文为博主原创文章,未经博主允许不得转载. 本程序通过 ...