这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!!

我们来看看API:

  1. #pragma mark - UICollectionViewDataSource
  2. // 指定Section个数
  3. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  4. return 3;
  5. }
  6. // 指定section中的collectionViewCell的个数
  7. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  8. return 10;
  9. }
  10. // 配置section中的collectionViewCell的显示
  11. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  12. CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
  13. cell.backgroundColor = [UIColor redColor];
  14. cell.textLabel.text = [NSString stringWithFormat:@"(%ld %ld)", indexPath.section, indexPath.row];
  15. return cell;
  16. }

看看直线布局的API:

  1. #pragma mark - UICollectionViewDelegateFlowLayout
  2. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  3. return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 - 10);
  4. }
  5. // 设置每个cell上下左右相距
  6. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  7. return UIEdgeInsetsMake(5, 5, 5, 5);
  8. }
  9. // 设置最小行间距,也就是前一行与后一行的中间最小间隔
  10. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  11. return 10;
  12. }
  13. // 设置最小列间距,也就是左行与右一行的中间最小间隔
  14. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  15. return 10;
  16. }
  17. // 设置section头视图的参考大小,与tableheaderview类似
  18. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  19. return CGSizeMake(self.view.frame.size.width, 40);
  20. }
  21. // 设置section尾视图的参考大小,与tablefooterview类似
  22. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  23. return CGSizeMake(self.view.frame.size.width, 40);
  24. }

如果是固定的,可以使用全局属性:

  1. NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewFlowLayout : UICollectionViewLayout
  2. @property (nonatomic) CGFloat minimumLineSpacing;
  3. @property (nonatomic) CGFloat minimumInteritemSpacing;
  4. @property (nonatomic) CGSize itemSize;
  5. @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -perferredLayoutAttributesFittingAttributes:
  6. @property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
  7. @property (nonatomic) CGSize headerReferenceSize;
  8. @property (nonatomic) CGSize footerReferenceSize;
  9. @property (nonatomic) UIEdgeInsets sectionInset;
  10. @end

常用到的代理方法:

  1. #pragma mark - UICollectionViewDelegate
  2. // 允许选中时,高亮
  3. - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
  4. NSLog(@"%s", __FUNCTION__);
  5. return YES;
  6. }
  7. // 高亮完成后回调
  8. - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
  9. NSLog(@"%s", __FUNCTION__);
  10. }
  11. // 由高亮转成非高亮完成时的回调
  12. - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
  13. NSLog(@"%s", __FUNCTION__);
  14. }
  15. // 设置是否允许选中
  16. - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  17. NSLog(@"%s", __FUNCTION__);
  18. return YES;
  19. }
  20. // 设置是否允许取消选中
  21. - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  22. NSLog(@"%s", __FUNCTION__);
  23. return YES;
  24. }
  25. // 选中操作
  26. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  27. NSLog(@"%s", __FUNCTION__);
  28. }
  29. // 取消选中操作
  30. - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
  31. NSLog(@"%s", __FUNCTION__);
  32. }

demo:https://github.com/632840804/CollectionViewDemo

UICollectionView的基本使用的更多相关文章

  1. 【iOS】Xcode8+Swift3 纯代码模式实现 UICollectionView

    开发环境 macOS Sierra 10.12.Xcode 8.0,如下图所示: 总体思路 1.建立空白的storyboard用于呈现列表 2.实现自定义单个单元格(继承自:UICollectionV ...

  2. 使用UICollectionView实现首页的滚动效果

    实现类似这样的效果,可以滚动大概有两种实现方案 1. 使用scrollview来实现 2. 使用UICollectionView来实现 第一种比较简单,而且相对于性能来说不太好,于是我们使用第二种方案 ...

  3. 用NSCalendar和UICollectionView自定义日历,并实现签到显示

    前一段时间因为工作需要实现了一个可以签到的日历,来记录一下实现的思路 效果如图:   这里的基本需求是: 1,显示用户某个月的签到情况,已经签到的日子打个圈,没有签到且在某个时间范围内的可以签到,其他 ...

  4. UICollectionView布局cell的三种方式

    UICollectionViewFlowLayout里面: // 方法一 - (void)prepareLayout{} // 方法二 - (nullable NSArray<__kindof ...

  5. 【Swift】iOS UICollectionView 计算 Cell 大小的陷阱

    前言 API 不熟悉导致的问题,想当然的去理解果然会出问题,这里记录一下 UICollectionView 使用问题. 声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cn ...

  6. UICollectionLayout布局 —— UIKit之学习UICollectionView记录二《流水布局》

    重点知识 一. 加载collectionView注意事项 1.创建collectionView,有两种方式 :一种是xib和一种是纯代码:设置代理和数据源,注册cell,配置流水布局的属性,如上.下. ...

  7. UICollectionView中使用 UICollectionViewFlowLayout进行布局(模仿苹果相册)

    现在都知道,在初始化UICollectionView的时候,必须要传入一Layout对象,进行布局管理.这也是collectionview和tableview的明显区别,通过collectionvie ...

  8. UI第十九节——UICollectionView

    UICollectionView其实就是UITableView的升级版,在布局方面比UITableView更出色.下面,先看代码吧 #import "RootViewController.h ...

  9. iOS6新特征:UICollectionView介绍

    http://blog.csdn.net/eqera/article/details/8134986 1.1. Collection View 全家福: UICollectionView, UITab ...

  10. 【iOS】UITabView/UICollectionView 全选问题

    UITabView/UICollectionView 全选问题 SkySeraph July. 30th 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySera ...

随机推荐

  1. FoxOne---一个快速高效的BS框架

    FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...

  2. Java和C++的不同

    现在一边继续深入C++,一边学习Java,为了学习得更加透彻,不断比较两者之间的不同,以后会慢慢继续增加. 1.在多态的实现上,C++需要利用关键字virtual,而Java不需要,因为在Java中, ...

  3. Android Studio使用教程(一)

    今年的Google全球开发者大会虽然没有新的Android系统和设备,但是还是推出了一些不错的产品,Android Studio就是其中之一.这个基于Intellij IDEA开发的Android I ...

  4. 高性能Java Web 页面静态化技术

    package com.yancms.util; import java.io.*; import org.apache.commons.httpclient.*; import org.apache ...

  5. System.Net网络编程--AuthenticationManager和IAuthenticationModule

    AuthenticationManager——管理客户端身份验证过程中调用的身份验证模块. public class Demo1 { private static string username, p ...

  6. (转)弹出窗口lhgDialog API文档

    应用到你的项目 如果您使用独立版本的lhgDialog窗口组件,您只需在页面head中引入lhgcore.lhgdialog.min.js文件,4.1.1+版本做了修改可以和jQuerya库同时引用, ...

  7. params参数的使用方法

    params 将方法中实际参数列表中跟可变参数数组类型一致的类型,都处理为数组中的的元素 static void Main(string[] arr) { // int[] numbers={2,3, ...

  8. dede调用指定的多个栏目导航

    {dede:channelartlist row=' typeid='1,2这里输入多个指定的栏目ID' } <li><a href='{dede:field name='typeu ...

  9. pyzmq简单的在线聊天室

    #encoding=utf-8#客户端 import zmq c = zmq.Context() s = c.socket(zmq.REQ) s.connect('tcp://127.0.0.1:10 ...

  10. 一些常用的js,jquerry 样例

    1 设置属性    $(document).attr("title", str) 2 删除属性在增加属性    $("#" + bottonname).remo ...