IOS基金会_ UICollectionView简单易用

IOS基金会_ UICollectionView简单易用

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

和表格视图类似 UICollectionView的使用有两种方法

一种是继承UICollectionViewController,这个Controller会自带一个UICollectionView。

第二种是创建一个UIConllectionView 视图放在普通的UIViewController里面。

我们用另外一种


首先声明先声明一个重用标示  和实现托付

#define _CELL @“acell”

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


然后初始化UICollectionVIew

– (void)initCollectionView

{

    //先实例化一个层

    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];

    

    //创建一屏的视图大小

    UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

    

    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:_CELL];

    collectionView.backgroundColor=[UIColor whiteColor];

    collectionView.delegate=self;

    collectionView.dataSource=self;

    

    [self.view addSubview:collectionView];

}


实现代理方法

#pragma mark –UICollectionViewDataSource

//定义展示的UICollectionViewCell的个数

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

{

    return 31;

}

//定义展示的Section的个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

//每一个UICollectionView展示的内容

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

{

    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:_CELL forIndexPath:indexPath];

    

    cell.backgroundColor = [UIColor colorWithRed:((arc4random()%255)/255.0) green:((arc4random()%255)/255.0) blue:((arc4random()%255)/255.0) alpha:1.0f];

    

    return cell;

}


#pragma mark –UICollectionViewDelegate

//UICollectionView被选中时调用的方法

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

{

    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];

    cell.backgroundColor = [UIColor colorWithRed:((arc4random()%255)/255.0) green:((arc4random()%255)/255.0) blue:((arc4random()%255)/255.0) alpha:1.0f];

}

//返回这个UICollectionViewCell能否够被选择

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

{

    return YES;

}



#pragma mark –UICollectionViewDelegateFlowLayout

//定义每一个UICollectionView 的大小

– (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return CGSizeMake(90, 90);

}

//定义每一个UICollectionView 的边距

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    return UIEdgeInsetsMake(10, 10, 10,10);

}


这样一个简单地UICollection视图就完毕了



版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116967.html原文链接:https://javaforall.net

(0)
上一篇 2022年1月12日 下午6:00
下一篇 2022年1月12日 下午6:00


相关推荐

  • 一篇教会你写90%的shell脚本_flash动画脚本怎么写

    一篇教会你写90%的shell脚本_flash动画脚本怎么写如果将运维比作手链,将珍珠比作服务,那么将珍珠串起来的线就是Shell,作为实现Linux系统自动管理以及自动化运维所必备的Shell脚本,其重要性便不言而喻。每一位合格的Linux系统管理员或运维工程师,都应该将熟练编写Shell脚本视为基本技能之一,只有这样才能减少不必要的劳动。但是新人在编写脚本时却只会:#!/bin/bash,所以今天就给大家分享100个经典实用的脚本范例,手把手教你写Shell脚本。这100个脚本范例从基础到进阶均涵盖,有侧重地提高编写能力,结构清晰准确,PDF格式,可

    2022年10月3日
    4
  • Java继承的概念及方法

    Java继承的概念及方法继承的概念继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。生活中的继承:兔子和羊属于食草动物类,狮子和豹属于食肉动物类。食草动物和食肉动物又是属于动物类。所以继承需要符合的关系是:

    2022年7月8日
    28
  • MySQL comment_mysql和oracle的区别

    MySQL comment_mysql和oracle的区别iconv用法场景说明将UTF-8的编码转换成GBK编码,使用Notepad++创建一个UTF-8(无BOM)的编码文件,文件内容简单为风雨在途,保存文件名称为utf8.txt,转换之后存储文件名称为gbk.txt.1.1:iconv命令的使用iconv-fUTF8-tGBKgbk.txt1.2:iconv函数的使用#includesize_ticonv(iconv_tcd…

    2025年8月21日
    4
  • Text Justification — LeetCode

    Text Justification — LeetCode

    2022年1月7日
    44
  • 通过ManagementObject获取当前打印机设备的状态

    通过ManagementObject获取当前打印机设备的状态首先声明一个打印机状态枚举:publicenumPrinterStatus{[Description("其他状态")]Other=1,[Description(&

    2022年7月2日
    26
  • mysql ,binlog 日志查询

    mysql ,binlog 日志查询文章目录一 开启 binlog 二 通过 mysql 的变量配置表 查看二进制日志是否已开启三 binlog 日志内容查看 1 mysql 查看 binlog2 找到 mysqlbinlog 命令 3 使用 mysqlbinlog 命令 4 mysqlbinlog 异常四 常用的 Binlog 操作命令一 开启 binlog 在 mysql 的配置文件 my cnf 开启 log bin 功能 log bin mysql bin 确认是打开状态 说明 值 mysql bin 是日志的基本名或前缀名 mysqld log bin

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号