新建View文件
![](https://img.haomeiwen.com/i3245733/75a3c71567a5cee0.png)
继承UICollectionView
![](https://img.haomeiwen.com/i3245733/2fa003b3076cf459.png)
0.为类新建相应测View文件
![](https://img.haomeiwen.com/i3245733/dca1cf6eab3891e1.png)
![](https://img.haomeiwen.com/i3245733/19aee081d53fa8f1.png)
关联控制器和View
![](https://img.haomeiwen.com/i3245733/43da70a700fdf733.png)
size设置为Freeform 可以自由调整大小
![](https://img.haomeiwen.com/i3245733/c849c6f4d8a73dfa.png)
设置高度为90
![](https://img.haomeiwen.com/i3245733/0d4241a7cabaed38.png)
为了能看到这个view,给view设置一个背景颜色
![](https://img.haomeiwen.com/i3245733/f8c3dbb212a5a193.png)
1.快速创建类方法 && 设置不跟随父控件的拉伸而拉伸
import UIKit
class RecommentGroupView: UIView {
override func awakeFromNib() {
super.awakeFromNib()
// 让控件不随着父控件的拉伸而拉伸
autoresizingMask = UIViewAutoresizing()
}
}
extension RecommentGroupView{
//提供快速创建类方法
class func recommentGroupView() -> RecommentGroupView{
return Bundle.main.loadNibNamed("RecommentGroupView", owner: nil, options: nil)?.first as! RecommentGroupView
}
}
![](https://img.haomeiwen.com/i3245733/e827e7f10e193cd8.png)
2.到对应的控制器中,通过闭包懒加载之前定义的这个View
//懒加载需要创建的View,通过闭包的方式
lazy var groupView:RecommentGroupView = {
//MARK:通过类方法来创建View
let groupView = RecommentGroupView.recommentGroupView()
// groupView.frame =
return groupView
}()
![](https://img.haomeiwen.com/i3245733/4614cc83d1303578.png)
3.到设置UI方法中将groupView添加到collectionView中
修改前
![](https://img.haomeiwen.com/i3245733/b470ccd61f0bf2a2.png)
修改后
![](https://img.haomeiwen.com/i3245733/7e338bcc30dcb87c.png)
4.先定义一个groupView 的高度。
在collectionView 添加的groupView后还不能显示,因为还没有设置frame以及调整之前的cycleView 的Y值
//定义 RecommentGroupView的高度
private let kGroupViewH : CGFloat = 90
![](https://img.haomeiwen.com/i3245733/7d815c0089c43303.png)
5.调整之前轮播的frame的Y值,Y变为 两个容器的高度之和
由原来的
cycleView.frame = CGRect(x: 0, y: - kCycleViewH , width: kScreenW , height: kCycleViewH)
改为:
cycleView.frame = CGRect(x: 0, y: -(kCycleViewH + kGroupViewH), width: kScreenW , height: kCycleViewH)
![](https://img.haomeiwen.com/i3245733/c02bfe8f4585ab21.png)
6.设置collectionViewron容器偏移量的时候,加上groupView的高度
由原来的:
//4.设置collectionView的内边距
collectionView.contentInset = UIEdgeInsets(top: kCycleViewH , left: 0, bottom: 0, right: 0)
变为:
//4.设置collectionView的内边距
collectionView.contentInset = UIEdgeInsets(top: kCycleViewH + kGroupViewH, left: 0, bottom: 0, right: 0)
![](https://img.haomeiwen.com/i3245733/b31623e36e4778eb.png)
7.设置groupView的Frame
groupView.frame = CGRect(x: 0, y: -kGroupViewH, width: kScreenW, height: kGroupViewH)
运行查看效果:
![](https://img.haomeiwen.com/i3245733/7eeec88116f9b259.png)
8. 给添加好的View 拖入CollectionView控件进行布局
![](https://img.haomeiwen.com/i3245733/895f887631cc3f5c.png)
给推入的CollectionView设置约束
![](https://img.haomeiwen.com/i3245733/6e18377bcfa98bfe.png)
9.设置collectionView的数据源为自己
![](https://img.haomeiwen.com/i3245733/da2fe0f643f3584a.png)
设置后为:
![](https://img.haomeiwen.com/i3245733/681322970bd9af66.png)
10.将XIB中的collectionView定义一个collectionView属性
![](https://img.haomeiwen.com/i3245733/97224dd2fea3a383.png)
11.注册Cell
声明一个常量
private let kGroupCellID = "kGroupCellID"
注册Cell
//注册cell
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kGroupCellID)
12.代码遵守数据源协议
// MARK 遵守Collection数据源协议
extension RecommentGroupView:UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 8
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGroupCellID, for: indexPath)
// MARK :为了能显示Cell,如果是奇数显示蓝色,偶数显示红色
cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.red : UIColor.blue
return cell
}
}
![](https://img.haomeiwen.com/i3245733/cb45871aeeade550.png)
运行查看效果:
滚动方向和cell大小不对
![](https://img.haomeiwen.com/i3245733/9c73eec5c5dbfa79.png)
13.设置cell的滚动方式,从上下滚动改为左右滚动
![](https://img.haomeiwen.com/i3245733/505dc64b08eacdcb.png)
设置间距为0
![](https://img.haomeiwen.com/i3245733/fb263f1a62671b6c.png)
![](https://img.haomeiwen.com/i3245733/c1b8b4b659b4630f.png)
继续调整:高度和宽度
![](https://img.haomeiwen.com/i3245733/d4c382aafcb8f965.png)
隐藏滚动条
![](https://img.haomeiwen.com/i3245733/55a1daa58b2ce904.png)
运行查看效果:
![](https://img.haomeiwen.com/i3245733/799586b0334daf81.png)
马哥27
14.将之前分组的数据展示到cell中
var groups : [GoodGroupModel]?
![](https://img.haomeiwen.com/i3245733/ba5cf5130e9b7221.png)
15.回到对应的Controller中将之前请求到的数据传递给groupView
//加载数据源
extension RecommendViewController {
fileprivate func loadData(){
//1.请求推荐数据
recommendVM.requestData {
self.collectionView.reloadData()
}
//2.将数据传递给groupView
self.groupView.groups = self.recommendVM.goodGroup
//2.请求轮播数据
recommendVM.requestCycleData {
self.cycleView.cycleModels = self.recommendVM.cycleModels
}
}
}
![](https://img.haomeiwen.com/i3245733/e4c4c35e5f9b1dd8.png)
按住Command + self.groupView.groups 中的groups 看是否能跳转到14的定义
16.回到14步的定义,修改为监听数据的改变让collectionView进行重新加载数据
修改前
var groups : [GoodGroupModel]?
修改后
var groups : [GoodGroupModel]? {
didSet{
collectionView.reloadData()
}
![](https://img.haomeiwen.com/i3245733/3043c47f5f61de59.png)
17 ,修改返回值,和动态cell内容
修改前
// MARK 遵守Collection数据源协议
extension RecommentGroupView : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 6
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGroupCellID, for: indexPath)
cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.blue : UIColor.red
return cell
}
}
修改后
// MARK 遵守Collection数据源协议
extension RecommentGroupView : UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return groups?.count ?? 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kGroupCellID, for: indexPath)
let group = groups![indexPath.item]
cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.blue : UIColor.red
return cell
}
}
![](https://img.haomeiwen.com/i3245733/9a9add8631f9ab66.png)
这时候请求的值已经存储在group中,下一步就是要解析这里值,并进行展现
18 自定义view来展现请求的值,新建View
选择view
![](https://img.haomeiwen.com/i3245733/8d0b7602298c6c45.png)
继承之UICollectionCell 并勾选创建XIB文件
![](https://img.haomeiwen.com/i3245733/1b89d4f8fb48af6f.png)
19 修改第12步通过XIB来注册
修改前
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: kGroupCellID)
修改后
//注册cell,修改位XIB注册
collectionView.register(UINib(nibName: "CollectionGroupCell", bundle: nil), forCellWithReuseIdentifier: kGroupCellID)
![](https://img.haomeiwen.com/i3245733/4856aeb92e23b9af.png)
20. 对第12步,对之前的cell 进行强转
![](https://img.haomeiwen.com/i3245733/795a31447c11dbf0.png)
21.到自定义View中,定义模型属性
![](https://img.haomeiwen.com/i3245733/e9cff2bc80e115a5.png)
修改前:
let group = groups![indexPath.item]
修改cell的赋值,修改后
cell.group = groups![indexPath.item]
![](https://img.haomeiwen.com/i3245733/58ce4a87c3c92278.png)
22 修改XIB自定义view来显示请求到的数值
设置之前cell 定义的高宽
![](https://img.haomeiwen.com/i3245733/6913dbf9bd920b1b.png)
拖入一个lable进行颜色和约束定位
![](https://img.haomeiwen.com/i3245733/e812204791de49c0.png)
给文字设置约束1
![](https://img.haomeiwen.com/i3245733/190def6941b5da01.png)
设置约束2
![](https://img.haomeiwen.com/i3245733/68768dbe8b132176.png)
网友评论