先放demo地址:github.com/KeWangKW/99…
- swift-----KSCollectionVC【继承自KSViewController】
- OC-----KOCollectionVC【继承自KOViewController】
- 使用与封装的TableView类似,不再赘述,请自行查看demo或上一篇
- 区别:
- kw_initData 方法中没有 style 属性了
- kw_initUI 可在此方法中修改collectionViewLayout,使用自定义的FlowLayout
封装 UICollectionViewFlowLayout
-
基于此链接的代码进行修改实现 ,改名为
KWFlowLayout- 增加了 HeaderView 悬停效果,可控制全部 header 悬停 或者 某个 header 悬停
- 整体竖向滚动,cell等宽,cell高度不固定
- 去掉了原来的代理方法,原代理方法使用的属性,全由系统代理方式控制
- 每个section 列数 根据系统方法设置的 cell宽度 和 列表宽度 计算
- 已处理不同的section分组内,展示的cell列数不同的效果
- 其余的按需要实现 UICollectionViewDataSource、 UICollectionViewDelegate 、UICollectionViewDelegateFlowLayout 的代理方法即可
-
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView
-
- 设置header 和 footer 的 View,如果某个section分组不需要 header 或者 footer ,就设置 return UICollectionReusableView()
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize
-
- 设置headerSize,不需要设置header的地方,设置返回的 height 为 0 即可
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize
-
- 设置footerSize,不需要设置footer的地方,设置返回的 height 为 0 即可
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
-
- 设置section分组边距
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
-
- 设置cell列间距
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
-
- 设置行间距
-
- shouldInvalidateLayout 方法,在需要设置悬停效果时必须 返回 true,然后在滑动时才会调用 layoutAttributesForSupplementaryView 方法,在此方法内处理headerview的位置,实现悬停
- let attri = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: indexPath) 【在处理悬停时 ,header有时候会被cell盖住,所以要处理视图层级问题,悬停时的header 的 attri.zIndex = 10086 ,跟随滚动时header 的 attri.zIndex =0 】
-
使用:代码由Swift实现,swift和OC都可以用这个 KWFlowLayout
-
使用:直接修改 collectionView.collectionViewLayout 即可
- swift:
- let layout = KWFlowLayout()
- layout.stickyHeaders = [0,1] //可控制某个header悬停
- layout.stickyAllHeader = true //全部header悬停
- collectionView.collectionViewLayout = layout
- OC:
- KWFlowLayout * layout = [[KWFlowLayout alloc]init];
- layout.stickyHeaders = @[@0,@1];
- layout.stickyAllHeader = YES;
- self.collectionView.collectionViewLayout = layout;
- swift:
-
效果展示: