当前位置: 首页 > news >正文

iOS - UICollectionView 瀑布流 添加表头视图的坑

 UICollectionView 瀑布流 添加表头视图的坑

首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tableview.tableHeaderView就可以了. collectionView 怎么添加这样的效果的呢

有两种思路

第一种:在collectionView的段头代理中设置 (只在第一段中设置)

第二种:改变 collectionView 的内延距离, 然后添加在内延空白的位置.

 

第一种

//  返回头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView =nil;
    //返回段头段尾视图
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HMCollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
        //添加头视图的内容
        header.backgroundColor = [UIColor redColor];
        reusableView = header;
        return reusableView;
    }
    //如果底部视图
    if (kind ==UICollectionElementKindSectionFooter)
    {
        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"forIndexPath:indexPath];
        footerview.backgroundColor = [UIColor purpleColor];
        reusableView = footerview;
        
    }
    return reusableView;
}

但是你会发现,使用瀑布流的时候,,Cell都是不同大小的布局,UICollectionViewFlowLayout是自定义的布局,collectionView的代理不会走,所以,这里就比较坑了。

怎么解决呢 就是在自定义的Layout中添加加一个 Header类型的 UICollectionViewLayoutAttributes就可以。然后我把瀑布流的Cell的起始位置从headerView的最大Y开始布局。这样设置之后,controllerView中的代理方法才会走,要记得注册头视图哦,不然会崩。

注册段头

    //注册段头部视图
    [collectionView registerClass:[HMCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];

    //xib

    [collectionView registerNib:[UINib nibWithNibName:@"HMCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];

 

自定义的布局 HMWaterflowLayout : UICollectionViewLayout

.h

@interface HMWaterflowLayout : UICollectionViewLayout

@property (nonatomic, assign) UIEdgeInsets sectionInset;
/** 段头的size */
@property (nonatomic, assign) CGSize headerReferenceSize;

/** 每一列之间的间距 */
@property (nonatomic, assign) CGFloat columnMargin;
/** 每一行之间的间距 */
@property (nonatomic, assign) CGFloat rowMargin;
/** 显示多少列 */
@property (nonatomic, assign) int columnsCount;

@property (nonatomic, weak) id<HMWaterflowLayoutDelegate> delegate;

@end

.m

#import <UIKit/UIKit.h>
@class HMWaterflowLayout;

@protocol HMWaterflowLayoutDelegate <NSObject>
- (CGFloat)waterflowLayout:(HMWaterflowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath;
@end#import "HMWaterflowLayout.h"

@interface HMWaterflowLayout();
/** 这个字典用来存储每一列最大的Y值(每一列的高度) */
@property (nonatomic, strong) NSMutableDictionary *maxYDict;

/** 存放所有的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
@end

@implementation HMWaterflowLayout

- (NSMutableDictionary *)maxYDict
{
    if (!_maxYDict) {
        self.maxYDict = [[NSMutableDictionary alloc] init];
    }
    return _maxYDict;
}

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        self.attrsArray = [[NSMutableArray alloc] init];
    }
    return _attrsArray;
}

- (instancetype)init
{
    if (self = [super init]) {
        self.columnMargin = 10;
        self.rowMargin = 10;
        self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        self.columnsCount = 3;
        //如果段头的高度不一致 可以仿照UICollectionViewFlowLayout的代理 自己写一个代理方法返回 CGSize
        self.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 140);
    }
    return self;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
    return YES;
}

/**
 *  每次布局之前的准备
 */
- (void)prepareLayout
{
    [super prepareLayout];
    
    // 1.清空最大的Y值
    [self.maxYDict removeAllObjects];

    for (int i = 0; i<self.columnsCount; i++) {
        NSString *column = [NSString stringWithFormat:@"%d", i];
        self.maxYDict[column] = @(self.sectionInset.top);
    }
    
    // 2.计算所有cell的属性
    [self.attrsArray removeAllObjects];
    
    //头部视图
    UICollectionViewLayoutAttributes * layoutHeader = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathWithIndex:0]];
    layoutHeader.frame =CGRectMake(0,0, self.headerReferenceSize.width, self.headerReferenceSize.height);
    [self.attrsArray addObject:layoutHeader];

    //item内容视图
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i<count; i++) {
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        [self.attrsArray addObject:attrs];
    }
}

/**
 *  返回所有的尺寸
 */
- (CGSize)collectionViewContentSize
{
    __block NSString *maxColumn = @"0";
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
        if ([maxY floatValue] > [self.maxYDict[maxColumn] floatValue]) {
            maxColumn = column;
        }
    }];
    
    //包括段头headerView的高度
    return CGSizeMake(0, [self.maxYDict[maxColumn] floatValue] + self.sectionInset.bottom + self.headerReferenceSize.height );
}

/**
 *  返回indexPath这个位置Item的布局属性
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 假设最短的那一列的第0列
    __block NSString *minColumn = @"0";
    // 找出最短的那一列
    [self.maxYDict enumerateKeysAndObjectsUsingBlock:^(NSString *column, NSNumber *maxY, BOOL *stop) {
        if ([maxY floatValue] < [self.maxYDict[minColumn] floatValue]) {
            minColumn = column;
        }
    }];
    
    // 计算尺寸
    CGFloat width = (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin)/self.columnsCount;
    CGFloat height = [self.delegate waterflowLayout:self heightForWidth:width atIndexPath:indexPath];
    
    // 计算位置
    CGFloat x = self.sectionInset.left + (width + self.columnMargin) * [minColumn intValue];
    CGFloat y = [self.maxYDict[minColumn] floatValue] + self.rowMargin;
    
    // 更新这一列的最大Y值
    self.maxYDict[minColumn] = @(y + height);
    
    // 创建属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    //把瀑布流的Cell的起始位置从headerView的最大Y开始布局
    attrs.frame = CGRectMake(x, self.headerReferenceSize.height + y, width, height );
    return attrs;
}

/**
 *  返回rect范围内的布局属性
 */
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    return self.attrsArray;
}



@end

 

 

 

第二种

self.collectionView  =  [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) collectionViewLayout:flowlayout];
self.collectionView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
UIImageView *imagev = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"015.png"]];
imagev.frame = CGRectMake(0, -50, 320, 50);
[self.collectionView addSubview: imagev];
[self.view addSubview: _collectionView];

 

提示: UICollectionReusableView 段头是可以自定义的 第一种比较好

注意:UITableView 有两个样式布局 1.Group 2.Plain (段头可以停留) 而UICollectionView 需要自己写布局 要想实现Plain (段头可以停留)的效果可以找一些网上的第三方的框架参考一下

 

转载于:https://www.cnblogs.com/junhuawang/p/6762600.html

相关文章:

  • 罗列几个Android插件化开发框架
  • Cocos2d-x中使用第三方so库
  • 用jquery制作一个简单的导航栏
  • Laravel中ajax添加CsrfToken的方法
  • python中定义class时self的理解
  • Linux双网卡聚合改造
  • 大型网站技术架构(二)--架构模式(转)
  • 常用的一些javascript小技巧
  • 什么是WCF
  • Windows Azure VM Role (7) 创建服务
  • 未来富豪,将出自这12大颠覆性领域
  • 《制造业中的机器人、自动化和系统集成》—— 1.4 机器人应用的发展
  • 《After Effects CC中文版超级学习手册》——1.6 小试身手:精彩制作有时很简单...
  • 《Axure RP8 网站和APP原型制作 从入门到精通》一2.4 权衡并制定功能的优先级...
  • 《OpenACC并行编程实战》—— 3.2 导语格式
  • [nginx文档翻译系列] 控制nginx
  • [译]Python中的类属性与实例属性的区别
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 「译」Node.js Streams 基础
  • 【腾讯Bugly干货分享】从0到1打造直播 App
  • 2017前端实习生面试总结
  • create-react-app项目添加less配置
  • emacs初体验
  • hadoop集群管理系统搭建规划说明
  • If…else
  • iOS 颜色设置看我就够了
  • JavaScript HTML DOM
  • leetcode386. Lexicographical Numbers
  • node学习系列之简单文件上传
  • PHP 7 修改了什么呢 -- 2
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • 搭建gitbook 和 访问权限认证
  • 大快搜索数据爬虫技术实例安装教学篇
  • 猴子数据域名防封接口降低小说被封的风险
  • 回顾 Swift 多平台移植进度 #2
  • 力扣(LeetCode)965
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 原生 js 实现移动端 Touch 滑动反弹
  • MPAndroidChart 教程:Y轴 YAxis
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • #pragma multi_compile #pragma shader_feature
  • (33)STM32——485实验笔记
  • (C++)栈的链式存储结构(出栈、入栈、判空、遍历、销毁)(数据结构与算法)
  • (pojstep1.1.1)poj 1298(直叙式模拟)
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (一)u-boot-nand.bin的下载
  • (幽默漫画)有个程序员老公,是怎样的体验?
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .mysql secret在哪_MySQL如何使用索引
  • .Net mvc总结
  • .net 无限分类
  • .NetCore 如何动态路由
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .NET连接数据库方式
  • .NET命令行(CLI)常用命令