博客
关于我
Section的背景色-UICollectionView
阅读量:760 次
发布时间:2019-03-23

本文共 5141 字,大约阅读时间需要 17 分钟。

<div#endif><div#endif><h2[sizeofContext

<divsqlite Milano

UICollectionView FlowLayout 自定义开发指南

<div

通过自定义 UICollectionView FlowLayout,我们可以为集合视图的每个组增加背景视图,设置不同的样式属性。以下将详细介绍自定义 FlowLayout 的实现步骤。

1. 类与协议定义

在实现自定义 FlowLayout 之前,我们需要定义两个类和一个协议:

  • JHCollectionViewFlowLayout:自定义的 FlowLayout 类,处理布局属性和背景设置。
  • JHCollectionViewDelegateFlowLayout:一个协议,定义委托方法,用于设置组背景颜色等属性。
  • JHCollectionReusableView:一个继承自 UICollectionReusableView 的自定义视图类,用于显示组背景。

2. 类实现细节

JHCollectionViewFlowLayout439>

初始化

初始化 JHCollectionViewFlowLayout 类时,注册自定义的 UICollectionReusableView 类作为组背景的装饰视图:

NSString *const JHCollectionViewSectionBackground = @"JHCollectionViewSectionBackground";@interface JHCollectionViewFlowLayout : UICollectionViewFlowLayout@property (nonatomic, strong) NSMutableArray *decorationViewAttrs;@end@implementation JHCollectionViewFlowLayout- (instancetype)init {    self = [super init];    if (self) {        self.decorationViewAttrs = [NSMutableArray array];        [self setup];    }    return self;}- (void)setup {    [self registerClass:[JHCollectionReusableView class] forDecorationViewOfKind:JHCollectionViewSectionBackground];}

获取背景颜色

prepareLayout 方法中,通过调用委托方法获取每个组的背景颜色,并为每个组创建对应的布局属性。

prepareLayout499>

[super prepareLayout];[self.decorationViewAttrs removeAllObjects];NSInteger numberOfSections = [self.collectionView numberOfSections];id денotes = self.collectionView.delegate;if (!numberOfSections || ![dentotes conformsToProtocol:@protocol(JHCollectionViewDelegateFlowLayout))] {    return;}for (NSInteger section = 0; section < numberOfSections; section++) {    NSInteger numberOfItems = [self.collectionView numberOfItemsInSection:section];    if (numberOfItems <= 0) {        continue;    }        // 获取第一个和最后一个项目的布局属性    UICollectionViewLayoutAttributes *firstItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];    UICollectionViewLayoutAttributes *lastItemAttr = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:numberOfItems - 1 inSection:section]];        if (!firstItemAttr || !lastItemAttr) {        continue;    }        // 获取组边距    UIEdgeInsets sectionInset = [self sectionInset];    if ([dentotes respondsToSelector:@selector(collectionView:layout:inlineInsetForSectionAtIndex:)]) {        sectionInset = [dentotes collectionView:self.collectionView layout:self insetForSectionAtIndex:section];    }        // 计算组的布局尺寸    CGRect sectionFrame = CGRectUnion(firstItemAttr.frame, lastItemAttr.frame);    if (self.scrollDirection == UICollectionViewScrollDirectionHorizontal) {        sectionFrame.size.width += sectionInset.left + sectionInset.right;        sectionFrame.size.height = self.collectionView.frame.size.height;    } else {        sectionFrame.size.height += sectionInset.top + sectionInset.bottom;        sectionFrame.size.width = self.collectionView.frame.size.width;    }        // 创建背景视图布局属性    JHCollectionViewLayoutAttributes *attr = [JHCollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:JHCollectionViewSectionBackground withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];    attr.frame = sectionFrame;    attr.zIndex = -1;    attr.backgroundColor = [dentotes collectionView:self.collectionView layout:self backgroundColorForSection:section];        [self.decorationViewAttrs addObject:attr];}

JHCollectionReusableView

自定义的装饰视图类,用于显示组背景:

@interface JHCollectionReusableView : UICollectionReusableView@end@implementation JHCollectionReusableView- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {    [super applyLayoutAttributes:layoutAttributes];        if ([layoutAttributes isKindOfClass:[JHCollectionViewLayoutAttributes class]]) {        JHCollectionViewLayoutAttributes *attr = (JHCollectionViewLayoutAttributes *)layoutAttributes;        self.backgroundColor = attr.backgroundColor;    }}

FlowLayout 继续改进

为了确保自定义布局能够正确显示,需要重写以下方法:

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {    NSArray *attrs = [[super layoutAttributesForElementsInRect:rect] mutableCopy];    for (UICollectionViewLayoutAttributes *attr in self.decorationViewAttrs) {        if (CGRectIntersectsRect(rect, attr.frame)) {            [attrs addObject:attr];        }    }    return attrs;}- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {    if ([elementKind isEqualToString:JHCollectionViewSectionBackground]) {        return [self.decorationViewAttrs objectAtIndex:indexPath.section];    }    return [super layoutAttributesForDecorationViewOfKind:elementKind atIndexPath:indexPath];}

3. 示例应用

在.storyboard 中定义自定义单元格,然后设置集合视图的 FlowLayout 参数:

  • isseurCell:设置为预定义的单元格类型。
  • rowCount:指定每个组的行数。
  • columnCount:指定每个组的列数。
  • sectionInset:设置组之间的边距。
  • insetForSectionAtIndex:通过委托方法设置不同组的内边距。

4. 常见问题

  • 如何设置不同的组背景颜色

    • 通过 JHCollectionViewDelegateFlowLayout 协议中的 backgroundColorForSection 方法,传递每个组的背景颜色。
  • 如何显示组背景图像

    • 将背景颜色替换为带有图像的颜色,并在自定义视图类中设置 backgroundColor
  • 如何添加复杂的样式属性(如圆角)?

    • JHCollectionViewLayoutAttributes 中添加额外的属性(如 corner Foods),并在自定义视图类中应用。

通过以上步骤,您可以成功实现自定义可留意 FlorenceLayout,为集合视图的每个组设置独特的背景样式和布局属性。

转载地址:http://lovzk.baihongyu.com/

你可能感兴趣的文章
MySQL:某个ip连接mysql失败次数过多,导致ip锁定
查看>>
MySQL:索引失效场景总结
查看>>
Mysql:避免重复的插入数据方法汇总
查看>>
MyS中的IF
查看>>
M_Map工具箱简介及地理图形绘制
查看>>
m_Orchestrate learning system---二十二、html代码如何变的容易
查看>>
M×N 形状 numpy.ndarray 的滑动窗口
查看>>
m个苹果放入n个盘子问题
查看>>
n = 3 , while n , continue
查看>>
n 叉树后序遍历转换为链表问题的深入探讨
查看>>
N!
查看>>
N-Gram的基本原理
查看>>
n1 c语言程序,全国青少年软件编程等级考试C语言经典程序题10道七
查看>>
Nacos Client常用配置
查看>>
nacos config
查看>>
Nacos Config--服务配置
查看>>
Nacos Derby 远程命令执行漏洞(QVD-2024-26473)
查看>>
Nacos 与 Eureka、Zookeeper 和 Consul 等其他注册中心的区别
查看>>
Nacos 单机集群搭建及常用生产环境配置 | Spring Cloud 3
查看>>
Nacos 启动报错[db-load-error]load jdbc.properties error
查看>>