博客
关于我
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原理、设计与应用全面解析
查看>>
MySQL原理简介—1.SQL的执行流程
查看>>
MySQL参数调优详解
查看>>
mysql参考触发条件_MySQL 5.0-触发器(参考)_mysql
查看>>
MySQL及navicat for mysql中文乱码
查看>>
MySqL双机热备份(二)--MysqL主-主复制实现
查看>>
MySql各种查询
查看>>
mysql同主机下 复制一个数据库所有文件到另一个数据库
查看>>
mysql启动以后会自动关闭_驾照虽然是C1,一直是开自动挡的车,会不会以后就不会开手动了?...
查看>>
mysql启动和关闭外键约束的方法(FOREIGN_KEY_CHECKS)
查看>>
Mysql启动失败解决过程
查看>>
MySQL启动失败:Can't start server: Bind on TCP/IP port
查看>>
mysql启动报错
查看>>
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>