转载自:   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. ios7.1 masonry布局中出现的问题

    UITextView中如果添加了子空间,比如自己添加一个placeHolder,placeHolder的Label使用masonry布局,会出现崩溃,原因是:在UITextView没有对子控件进行布局 ...

  2. 笨方法学python--打印

    1 print 语句 尾端的逗号 end1 = "C" end2 = "h" end3 = "e" end4 = "e" ...

  3. 介绍XXTEA加密算法及其C实现

    介绍XXTEA加密算法及其C实现 http://en.wikipedia.org/wiki/XXTEA “微型加密算法(TEA)及其相关变种(XTEA,Block TEA,XXTEA)都是分组加密算法 ...

  4. Java-Spring MVC如何返回一个非JSP文件名字的地址

    return new ModelAndView("redirect:/bizitem/goEditItem.do?item_id="+item_id+"&msg= ...

  5. 相对路径json无法获取

    在获取json文件内容时,有时会出现相对路径上的json文件无法获取的情况,Chrome上的解决方案如下: 1.右击桌面上的Chrome浏览器图标,点击属性; 2.在目标输入框的内容后头加上--arg ...

  6. echarts学习总结(一):图表溢出窗口,图表数据窗口显示不全

    如上图所示:echarts图形左面的数据没有完全显示 只需在option中定义一下grid,例如:                                                gri ...

  7. linux进程、管道和重定向

    1.shell先后使用fork和exec系统调用来执行一个外部命令. 2.在linux系统中,有三个文件会被内核自动打开,分别是stdin.stdout.stderr. 3.进程的属性相关命令: 查看 ...

  8. CodeForces 429 B B. Working out

    Description Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to loo ...

  9. jq中的移除事件

    unbind([type],[data]); 第一个参数是事件类型,第二个参数是将要移出的函数 说明: 1,如果没有参数,则删除所有绑定的事件 2,如果只提供了事件类型作为参数,则只删除该类型的绑定事 ...

  10. ecb gud

    mx gdb不好用,好像是因为用了一个dedicated window,改用mx gud-gdb,好用. http://stackoverflow.com/questions/13560933/how ...