在项目中看到有使用多种collectionViewCell,和header的情况,感觉用起来还是比较方便。之前遇到类似的界面,都没有想到😂.
看下效果图吧:
tu.gif
先说下思路,这整个一块是一个collectionView,然后分为3组
第一组:十点便宜图片是图+一个label为headerA,然后cellA的个数返回0
第二组:详细信息的label为headerB,然后cellB返回10个。
第三组:推荐为headerC,然后cellC返回50个。
当然其中的透视图和cell都是自定义的,这里我用的xib比较快可以达到我想要的效果。

1.再来看看实现的过程,首先是实例化collectionView,然后给他一个默认的布局方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
let flow:UICollectionViewFlowLayout = {
let flow = UICollectionViewFlowLayout()
flow.itemSize = CGSize(width: 50, height: 50)
flow.minimumLineSpacing = 1;
flow.minimumInteritemSpacing = 1;
flow.scrollDirection = .vertical;
return flow
}()
var collection:UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
//初始化
collection = UICollectionView(frame: view.bounds, collectionViewLayout: flow)
collection.dataSource = self;
collection.delegate = self;
collection.backgroundColor = UIColor.white
view.addSubview(collection)

//MARK:-----------注册cell-------------//
collection.register(UINib(nibName: "CellA", bundle: nil), forCellWithReuseIdentifier: "cellA")
collection.register(UINib(nibName: "CellB", bundle: nil), forCellWithReuseIdentifier: "cellB")
collection.register(UINib(nibName: "CellC", bundle: nil), forCellWithReuseIdentifier: "cellC")

//MARK:-----------注册Header-------------//
collection.register(UINib(nibName: "HeaderA", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headA")
collection.register(UINib(nibName: "HeaderB", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headB")
collection.register(UINib(nibName: "HeaderC", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headC")
}

2.再到我们的拓展里面来实现代理,这里要注意的是要遵守

UICollectionViewDelegateFlowLayout代理,然后才能进行自定义的布局的cell大小、header的宽高、cell之间的边距。然后就是根据不同的组返回不同的cell和header了。还是比较简单。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extension ViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

//布局的代理
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch indexPath.section {
case 0:
return CGSize.zero
case 1:
return CGSize(width: 100, height: 50)
case 2:
return CGSize(width: (UIScreen.main.bounds.width-30) / 2.0, height: 100)
default:
return CGSize.zero
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
switch section {
case 0:
return CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 2.0)
case 1:
return CGSize(width: UIScreen.main.bounds.width, height: 50)
case 2:
return CGSize(width: UIScreen.main.bounds.width, height: 100)
default:
return CGSize.zero
}
}
//自定义头头
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
switch indexPath.section {
case 0:
let headA = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headA", for: indexPath)
headA.backgroundColor = UIColor.cyan
return headA;
case 1:
let headB = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headB", for: indexPath)
headB.backgroundColor = UIColor.gray
return headB;
default:
let headC = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headC", for: indexPath)
headC.backgroundColor = UIColor.brown
return headC;
}


}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

switch indexPath.section {
case 0:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellA", for: indexPath)
return cell
case 1:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellB", for: indexPath)
return cell
default:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellC", for: indexPath)
return cell
}


}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 { return 0 }
if section == 1 { return 10 }
return 20
}

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 3
}
}
相关文章
评论
分享
  • 删除视频背景音乐、添加视频背景音乐

    删除视频背景音乐删除视频的背景音乐其实很简单,把视频数据导入进来,然后我们的视频数据其实是有两条轨道一条是音频,另外一条是视频。由于数据的tracks(轨道包含多条音视频)是只读的,所以在这里重新创建一个混合器,然后把数据源的视频轨道...

    删除视频背景音乐、添加视频背景音乐
  • AAC音频转WAV

    将aac编码格式的音频转为lpcm编码的音频,转出来的音频会比原来大个10倍左右.1234567891011121314151617181920212223242526272829303132333435363738394041424...

    AAC音频转WAV
  • Message Filtter Extension 短信过滤

    手机最近老是收到各种垃圾短信,于是想要自己写一个app来过滤垃圾短信,找到了Message Filtter Extension 1.首先创建一个空的工程,然后创建好了之后添加一个target。1选中工程->Editor-&g...

    Message Filtter Extension 短信过滤
  • Shell删除所有苹果证书

    123456789101112131415161718#!/bin/bashexpired=$(security find-identity)if [ -z "$expired" ] #-z判断字符串长度是否为0 为0返回tru...

    Shell删除所有苹果证书
  • Swift语言国际化

    第一步在工程设置里面添加对应国际化的语言 第二步添加Localizble.strings 第三步选择Localizble.strings文件,然后选择Localize…选择需要的语言,选择完了之后你就会看见刚才创建的Localizbl...

    Swift语言国际化
  • Docker Nginx 静态资源部署

    1. 查看docker仓库中的nginx命令1docker search nginx 2.为选定需要pull到系统重的官方Nginx镜像1docker pull nginx 这个过程需要时间,需要耐心等待一下。看到这个图就是成功了。 ...

    Docker Nginx 静态资源部署
  • Nginx vue打包部署

    最近在弄vue的项目,完了需要打包部署到服务器,然后之前也没有弄过自己在这边一边查资料一边琢磨怎么弄。幸运的是花了一天半的时间还搞定了。 1.在conf.d文件下添加一个新的配置文件,12345678910111213141516...

    Nginx vue打包部署
  • SCP文件上传下载

    从服务器获取文件1scp -p 8080 root@192.168.1.1:/usr/temp/file.txt /Desktop/ 上传文件到服务器1scp -p 8080 /Desktop/uploadfile root@192....

    SCP文件上传下载
  • 音频剪切、合成、淡入淡出

    音频剪切1.音频剪切比较简单传入音频文件和剪切的时间段就可以了12345678910111213141516171819202122232425/** 初始化输出文件路径 */ NSString *url = "音频源文件路径"...

    音频剪切、合成、淡入淡出
  • swift 做一个答题的功能

    实现的效果是:1.点击答案,判断正确,如果选择的答案错误显示出正确的答案。然后可以浏览上一题的信息。2.可以查看上一题的答题情况。看下效果图吧 思路:1.看看题的模型吧,我这里是用一个数组来装的一个类,然后点击下一题的时候数组索引+...

    swift 做一个答题的功能