iOS大典之集合视图

2023-05-16


项目遇到了集合视图,
感觉得整理下

又来一篇


遵守协议

@interface RootViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

// 创建一个集合视图
- (void)addSubViews
{

    // Item 布局 (网格状布局)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    // 行边距
    layout.minimumLineSpacing = 30;
    // 列边距
    layout.minimumInteritemSpacing = 30;

    // 设置item的宽高
    layout.itemSize = CGSizeMake(350, 450);

    // 设置滑动方向(默认是上下滑动)
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    // 设置表头 只有高度影响表头
    layout.headerReferenceSize = CGSizeMake(0, 200);
    // 设置表尾 只有高度影响表尾
    layout.footerReferenceSize = CGSizeMake(0, 100);

    // 设置内边距
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);

    // 初始化 集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; // 需要一个布局,先初始化一个

    //设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;

    // collectionView 默认是黑色
    collectionView.backgroundColor = [UIColor cyanColor];

    // 显示视图
    [self.view addSubview:collectionView];
    [collectionView release];
    [layout release];

    // 注册要用的cell
    // Identifier 重用标示符 要一致
    // <#(Class)#> 你的cell是那个类的 就添加那个类
    // 使用系统的就注册系统的 如果自定义的话 就注册自定义的
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];



    // 注册表头
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader"];

    // 注册表尾
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter"];


}



#pragma mark -- dataSource --


// 返回分区数, 默认就1个分区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 返回每个Item的方法
    // 必须有一步: 注册cell

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];


    // 使用UICollectionViewCell 一般都是自定义再使用 和tableView 一样 所有的自定义控件 都要加在contentView 上面
    cell.contentView.backgroundColor = [UIColor grayColor];

    return cell; // 不用释放

}


// 设置表头表尾 同代理方法 来实现

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 判断返回表头 还是表尾
    // 因为参数是字符串的 ,判断是否相同不能用等号
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

        // 返回表头 需要去复用集合中得到
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeader" forIndexPath:indexPath];
        // 加个颜色
        headerView.backgroundColor = [UIColor greenColor];
        return headerView;

    }else{
        // 返回表尾
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooter" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor yellowColor];
        return footerView;

    }
}



over

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS大典之集合视图 的相关文章

随机推荐

  • iOS开发笔记之NSFileManager的使用

    对于文件的管理 xff0c 从项目需求中出发 xff0c 有如下的学习成果 查看文档基本能完成基本的需求 文档中部分常用的方法 xff1a xff08 基本基于path和URL成对存在 xff0c 这里主要解读关于path的 xff09 获
  • 基于 FFmpeg 的播放器 demo

    这里的播放器演示程序用于播放一个本地文件 xff0c 因而不需要关心播放网络上的媒体数据时的网络传输问题 对于播放本地媒体文件的播放器来说 xff0c 所要完成的工作主要包括 xff1a 解封装 gt 音频解码 视频解码 gt 对于音频来说
  • mysql查看当前使用的配置文件my.cnf的方法

    1 查看是否使用了指定目录下的my cnf my cnf是mysql启动时加载的配置文件 xff0c 一般会放在mysql的安装目录中 xff0c 用户也可以放在其他目录加载 安装mysql后 xff0c 系统中会有多个my cnf文件 x
  • JUC中对线程的协同合作控制

    线程的协同合作控制 CountDownLatch使用代码演示小结 Semaphore使用小结 Condition使用代码 CyclicBarrier使用 xff1a 代码演示 小结 在使用多线程的时候 xff0c 我们可以使用一些工具来达到
  • Ubuntu+Anaconda+TA-Lib

    查看ubuntu版本 span class token function cat span etc issue 更换apt源 1 备份原有软件源 sudo cp etc apt sources list etc apt sources li
  • 服务器配置公网ftp服务端(软件和python代码两种方法)

    FileZilla Server超详细配置 前言一 配置教程1 General settings xff08 常规设置 xff09 2 Passive mode settings xff08 被动传输模式设置 xff09 3 Securit
  • 使用FFmpeg生成高清gif图

    前言 使用FFmpeg能够很方便的给视频片段或GIF加水印 xff0c 同时还能对选取的片段生成GIF图 xff0c 但是在使用默认FFmpeg设置情况下 xff0c 生成的GIF画质很差 xff0c 有很明显的栅格化现象 如何生成高质量的
  • PostgreSQL的SSL部署

    随着云服务器的兴起 xff0c 越来越多的数据库服务器被安装在远程 用SSL连接代替明文连接 xff0c 是数据库的基本安全功能 很庆幸PostgreSQL很早就支持openSSL xff0c 各发行版本都带有openSSL连接库 xff0
  • 八数码问题是否有解

    八数码问题 描述 xff1a 3 3的棋盘有八个数字 xff08 1 8 xff09 和一个空位 xff0c 数字可以滑动 问题研究的是从一个棋盘状态到另一个状态 是否有解的判定 12345678 对于上面的棋盘状态 xff0c 我们可以表
  • xrdp和vnc的区别

    在很多场景下 xff0c 我们需要远程连接到Linux服务器 本文是Ubuntu xff0c 传统的连接主要分为两种 第一种 xff1a 通过SSH服务 xff08 使用xshell等工具 xff09 来远程访问 xff0c 编写终端命令
  • 和风天气API数据分析

    注册和风天气获取key 请求数据时需要用到 xff0c 具体可查看文档 请求数据示例 3 10天天气预报 34 HeWeather6 34 34 basic 34 34 cid 34 34 CN101010100 34 34 locatio
  • 提供python接口有用的库

    python版本 windows上使用的是python2 7 2 32位版本 如何查看python是32位还是64位 xff1a import struct struct calcsize 34 P 34 如果是4 xff0c 说明是32位
  • Linux添加库路径的两种方法

    库文件在连接 静态库和共享库 和运行 仅限于使用共享库的程序 时被使用 xff0c 其搜索路径是在系统中进行设置的 一般 Linux 系统把 lib 和 usr lib 两个目录作为默认的库搜索路径 xff0c 所以使用这两个目录中的库时不
  • 动态库和静态库在编译C语言时使用方法简述

    函数库分为静态库和动态库两种 创建Linux静态库和Linux动态库和使用它们在这里将以举例的形式详述一下 静态库在程序编译时会被连接到目标代码中 xff0c 程序运行时将不再需要该静态库 动态库在程序编译时并不会被连接到目标代码中 xff
  • Linux下的tar压缩解压缩命令详解

    tar c 建立压缩档案 x xff1a 解压 t xff1a 查看内容 r xff1a 向压缩归档文件末尾追加文件 u xff1a 更新原压缩包中的文件 这五个是独立的命令 xff0c 压缩解压都要用到其中一个 xff0c 可以和别的命令
  • Linux环境下安装MATLAB2012

    在GNU Debian环境下 xff0c 使用iso镜像安装Matlab2012 xff0c 首先需要用mount命令挂载镜像 xff0c mount o loop t iso9660 source mount point 具体的讲 mou
  • string与string.h以及cstring的关系区别

    c 43 43 中 string与string h 的作用和区别 如下代码 include lt string h gt void main string aaa 61 34 abcsd d 34 printf 34 looking for
  • gcc与g++的比较以及库的使用说明

    一 xff1a gcc与g 43 43 比较 编 译c c 43 43 代码的时候 xff0c 有人用gcc xff0c 有人用g 43 43 xff0c 于是各种说法都来了 xff0c 譬如c代码用gcc xff0c 而c 43 43 代
  • makefile 中的 wildcard 与 patsubst 函数

    makefile 里的函数跟它的变量很相似 使用的时候 xff0c 你用一个 符号跟开括号 xff0c 函数名 xff0c 空格后跟一列由逗号分隔的参数 xff0c 最后用关括号结束 例如 xff0c 在 GNU Make 里有一个叫 39
  • iOS大典之集合视图

    项目遇到了集合视图 感觉得整理下 又来一篇 遵守协议 span class hljs class span class hljs keyword 64 interface span span class hljs title RootVie