博客
关于我
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 主从同步文档的大坑
查看>>
mysql 主键重复则覆盖_数据库主键不能重复
查看>>
Mysql 事务知识点与优化建议
查看>>
Mysql 优化 or
查看>>
mysql 优化器 key_mysql – 选择*和查询优化器
查看>>
MySQL 优化:Explain 执行计划详解
查看>>
Mysql 会导致锁表的语法
查看>>
mysql 使用sql文件恢复数据库
查看>>
mysql 修改默认字符集为utf8
查看>>
Mysql 共享锁
查看>>
MySQL 内核深度优化
查看>>
mysql 内连接、自然连接、外连接的区别
查看>>
mysql 写入慢优化
查看>>
mysql 分组统计SQL语句
查看>>
Mysql 分页
查看>>
Mysql 分页语句 Limit原理
查看>>
MySql 创建函数 Error Code : 1418
查看>>
MySQL 创建新用户及授予权限的完整流程
查看>>
mysql 创建表,不能包含关键字values 以及 表id自增问题
查看>>
mysql 删除日志文件详解
查看>>