首页 技术 正文
技术 2022年11月15日
0 收藏 750 点赞 3,233 浏览 2192 个字

具体代码如下:

1、声明

 var hCollectionView:UICollectionView?
var layout:UICollectionViewFlowLayout? let courser = [
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"],
["name":"jack chen","pic":"1.jpeg"]
]

2、创建流布局,并初始化cellevtionView

//        设置布局流格式
layout = UICollectionViewFlowLayout()
layout?.itemSize = CGSize(width:self.view.frame.width/-,height:)
// 列间距,行间距,偏移
layout?.minimumInteritemSpacing = //列间距
layout?.minimumLineSpacing = //行间距
// layout?.sectionInset = UIEdgeInsets(top:5,left:0,bottom:0,right:0) self.hCollectionView = UICollectionView.init(frame: CGRect(x:,y:,width:kScreenWidth,height:kScreenHeight), collectionViewLayout: layout!)
self.hCollectionView?.delegate = self
self.hCollectionView?.dataSource = self
self.hCollectionView?.register(UINib.init(nibName: "hCell", bundle: nil), forCellWithReuseIdentifier: "hCell")
self.hCollectionView?.backgroundColor = UIColor.orange
//collectionview的偏移量
// self.hCollectionView?.contentInset = UIEdgeInsets(top:5,left:0,bottom:5,right:0) self.view.addSubview(self.hCollectionView!)

3、代理方法的实现

//    行数
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return courser.count
}// 获取单元格
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell:hCell = self.hCollectionView?.dequeueReusableCell(withReuseIdentifier: "hCell", for: indexPath) as! hCell
//这里判断了下奇偶数
if indexPath.item% == {
cell.backgroundColor = UIColor.red
cell.bgImg.image = UIImage(named:self.courser[indexPath.item]["pic"]!)
cell.descLab.text = self.courser[indexPath.item]["name"] }else{
cell.backgroundColor = UIColor.yellow
cell.bgImg.image = UIImage(named:self.courser[indexPath.item]["pic"]!)
cell.descLab.text = self.courser[indexPath.item]["name"]
}

最终效果图:

swift – 之 UICollectionView的用法/自定义流布局

上面这种的话,是标准的流布局,设定好item的尺寸,会随着屏幕向下平铺,但是还有一种自定义的流布局,例如一些门户类app的首页,左边有一个大的Item,右边是2个小的item,两个小的总高度和一个大的Item的高度是一致的!其实就是几个方法,东西不难,敲几遍都差不多了,这里仅做记录!

实现自定义流布局的话,主要是实现这3个方法:

// 内容区域总大小,不是可见区域
override var collectionViewContentSize: CGSize
// 所有单元格位置属性
override func layoutAttributesForElements(in rect: CGRect)
-> [UICollectionViewLayoutAttributes]?
// 这个方法返回每个单元格的位置和大小
override func layoutAttributesForItem(at indexPath: IndexPath)
-> UICollectionViewLayoutAttributes?
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,089
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,566
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,415
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,187
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,823
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,906