转载自:   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. Photoshop 钢笔 双窗口显示

    钢笔:两点成一线  两点:Photoshop 叫锚点 中间线叫做路径 路径变选区 Crtl+Enter 窗口 >>>排列>>> 双联垂直 两个文件同时移动: shi ...

  2. ASP.NET中使用Server.Transfer()方法在页间传值 实例

    以下代码在VS2008中测试通过 <%@ Page Language="C#" AutoEventWireup="true" CodeFile=" ...

  3. Zookeeper: configuring on centos7

    thispassage is referenced, appreciated. ZooKeeper installation: Download from this site Install java ...

  4. git 强制覆盖本地文件

    git fetch --all git reset --hard origin/master git pull

  5. POJ 2635 The Embarrassed Cryptographer(大数求余)

    题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...

  6. 学习笔记——建造者模式Builder

    构造者模式.外部场景如果需要一个汽车类,它不需要关心如何构造,它只需要告诉Director需要什么,就可以从Director获得. 如:CDirector(IBuilder* aBuilder); 场 ...

  7. mysql 查询 45 道题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  8. android:contentDescription的作用是什么

    在写Android的XML布局文件时,在ImageView或ImageButton中经常会碰到一个提示: Missing contentDescription attribute on image. ...

  9. mysql笔记7之数据类型

    1 区别一: varchar:可变长度的字符串.根据添加的数据长度决定占用的字符数 char:固定长度的字符串 2区别二 int:没有限制 int(4):限制为4 3 区别三: 日期: date    ...

  10. 别说你不知道java中的包装类,wrapper type,以及容易在自动拆箱中出现的问题

    很多时候,会有人问你,你知道什么是包装类吗? 或者高端一点问你你知道,wrapper type,是什么吗? 然后你就懵逼了,学了java很多时候都不知道这是啥. 其实问你的人,可能只是想问你,java ...