美文网首页
学习笔记-集合视图创建(初学)

学习笔记-集合视图创建(初学)

作者: 听雨花春风 | 来源:发表于2016-04-26 15:22 被阅读71次
集合视图 collectionView

1.dataSource delegate

2.显示单位:单元格cell UICollectionViewCell 一行多个单元格

3.indexPath:组section+下标row/item

使用方法:
  • 创建布局类
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
  /**
   *  布局:cell与空间边界无空隙

      垂直滚动:
          cell间的水平间隔相等,间隔宽度取决于:(集合视图宽度-所有cell的宽度和)/间隔数
          cell间的垂直间隔相等,间隔高度取决于:最小垂直间隔
      水平滚动:
          cell间的水平间隔相等,间隔宽度取决于:最小水平间隔
          cell间的垂直间隔相等,间隔高度取决于:(集合视图高度-所有cell的高度和)/间隔数


   */
  //cell的大小✅
  layout.itemSize = CGSizeMake(80, 80);
  //垂直方向的cell最小间隔
  layout.minimumLineSpacing = 10;
  //水平方向的cell最小间隔
  layout.minimumInteritemSpacing = 10;
  //滑动方向 水平/垂直
  layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  //组头/尾视图的尺寸
  layout.headerReferenceSize = CGSizeMake(100, 100);
  layout.footerReferenceSize = CGSizeMake(100, 100);

  • 创建集合视图
//集合视图
  UICollectionView *collection = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];

  [self.view addSubview:collection];

  • 实现数据源和代理方法
//实现数据源协议方法和代理协议方法
  collection.delegate = self;
  collection.dataSource = self;


  • 定制单元格
    • 注册单元格 子类化/原生
//4注册:表视图,集合视图都可以使用

       //1.类->注册
   [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"class_cell"];

       //2.Xib->注册
//    [collection registerNib:[UINib nibWithNibName:@"xib文件名" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"xib_cell"];

       //3.类->注册头/尾视图

       //注册头视图 UICollectionElementKindSectionHeader
   [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"class_header"];
       //注册尾视图 UICollectionElementKindSectionFooter
   [collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"class_footer"];

       //4.Xib->注册头/尾视图
//    [collection registerNib:[UINib nibWithNibName:@"xib文件名" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"xib_footer"];

UICollectionView的代理方法大全


首先,有两个方法是我们必须实现的:

设置分区数

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



设置返回每个item的属性


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



下面的方法是可选实现的:

虽然这个方法是可选的,一般我们都会去实现,设置每个分区的item个数

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

对头视图或者尾视图进行设置

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;



设置某个item是否可以被移动,返回NO则不能移动


- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);



移动item的时候,会调用这个方法


- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;

三、UICollectionViewDelegate协议

        这个协议用来设置和处理collectionView的功能和一些逻辑,所有方法都是可选实现:

是否允许某个Item的高亮,返回NO,则不能进入高亮状态

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;



当item高亮时触发的方法

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;



结束高亮状态时触发的方法

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;



是否可以选中某个Item,返回NO,则不能选中

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;



是否可以取消选中某个Item

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath;



已经选中某个item时触发的方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;



取消选中某个Item时触发的方法

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;



将要加载某个Item时调用的方法

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);



将要加载头尾视图时调用的方法

- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);



已经展示某个Item时触发的方法

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;



已经展示某个头尾视图时触发的方法

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;



这个方法设置是否展示长按菜单

- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;

长按菜单中可以触发一下类复制粘贴的方法,效果如下:




这个方法用于设置要展示的菜单选项

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender;



这个方法用于实现点击菜单按钮后的触发方法,通过测试,只有copy,cut和paste三个方法可以使用

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender;

通过下面的方式可以将点击按钮的方法名打印出来:

?
1
2
3
-(void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
    NSLog(@"%@",NSStringFromSelector(action));
}


collectionView进行重新布局时调用的方法

- (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout;


发现问题:

练习中发现collection的集合的头视图上的lable 会发生重叠,即多次加载lable,导致重叠.

解决方法:
将头视图子类化

或者移除之前加载的控件

相关文章

  • 学习笔记-集合视图创建(初学)

    集合视图 collectionView 1.dataSource delegate 2.显示单位:单元格cell ...

  • UICOllectionView-集合视图

    一、集合视图的概念 1、如何创建UIColletionView 2、集合视图的布局UICollectionView...

  • MVC简述

    创建模型 let M = Backbone.Model.extend({}) 创建集合 创建视图 Backbone...

  • iOS开发UI阶段——第十五节 UICollectionView

    集合视图的创建必须指定布局,如果没有布局,则显示不了任何东西,所以在创建集合视图对象之前必须先创建一个布局对象 采...

  • UICollectionView(集合视图学习笔记)

    集合视图的作用 管理数据项的有序集合并使用可自定义布局显示它们的对象。集合视图是为了增强网格视图开发而在IOS6中...

  • CollectionView

    //集合视图 //创建全局静态重用标识符 static NSString * collectionID = @"i...

  • 19.03.24 定义URL

    # Django 18.3创建网页:学习笔记主页 * 创建网页过程三个阶段: 1. 定义URL 2. 编写视图 3...

  • Django Web应用程序(三)

    18.3 创建网页: 学习笔记主页 使用Django创建网页的过程通常分为三个阶段: 定义URL、编写视图和编写模...

  • 【用树莓派搭建Django网站3】创建匿名访问页

    极客时间的吕老师Django课程的学习笔记 模板文件夹 创建base模板页 创建列表页 创建模板 在视图层中加入自...

  • oracle--数据库对象

    数据库对象 一、视图:从表中抽出的逻辑上相关的数据集合 要创建视图,需要管理员授予权限: 视图: 视图的优点: 创...

网友评论

      本文标题:学习笔记-集合视图创建(初学)

      本文链接:https://www.haomeiwen.com/subject/zjgqrttx.html