转载自:   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. Linux系统的信号详解

    一.信号类型 1) SIGHUP       2) SIGINT       3) SIGQUIT     4) SIGILL        5) SIGTRAP 6) SIGABRT      7) ...

  2. windows的DOS窗口如何修改大小

    关于这个问题,其实很简单.不知道为什么网上的资料乱遭的.故自己写下来,方便有不明白的童鞋参考. 左键点击左上角的区域会弹出一个菜单,选择属性. 如下图就能轻松的修改窗口的大小了.

  3. php添加gd

    一 GD简介: php处理图形的扩展库,提供了一系列用来处理图片的API.如果开发过程中发现有页面验证码不能显示,则要考虑检查phpinfo(),是否支持GD库. 二 思路: 网上发现添加GD库的方法 ...

  4. Linq序列间的操作

    一.Union并集操作相同的数据只显示一次 二.Concact连接 相同的数据也会重复显示 三.except差集 我有的你没有的显示 四.intersect交集相同的显示 五.zip序列的合并 六.d ...

  5. C#操作SQLite 报错 (Attempt to write a read-only database)

    解决办法:找到SQLite数据库所在的文件夹,单击右键,属性->安全,为Users用户组添加写入权限.

  6. 8.3 sikuli 集成进eclipse 报错:eclipse中运行提示 Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform

    sikuli运行出现问题:Win32Util.dll: Can't load 32-bit .dll on a AMD 64 bit platform 在64位平台上无法加载32位的dll文件 解决办 ...

  7. cakephp 复杂查询

    $now = time(); $this->CardTypeInfos->recursive = -1; $conditions = [ 'seller_id'=>SELLER_ID ...

  8. skia入门

    SkBitmap bmp; bmp.setConfig(SkBitmap::kARGB_8888_Config, rect.Width(), rect.Height()); bmp.allocPixe ...

  9. Swift -> Optional嵌套 探讨

    准备运动:Optional 的介绍 王巍的<Swifter>一书中,介绍了一个有用的命令:在 LLDB 中输入 fr v -R foo,可以查看foo 这个变量的内存构成.我们稍后的分析将 ...

  10. Co-Debugging JNI with Android Studio and Visual Studio

    Tutorials > Android > Integration with other tools > Co-Debugging JNI with Android Studio a ...