UICollectionview实现类似UITableview的sectionheader悬浮代码
2015-07-16 10:51
756 查看
UICollectionview实现类似UITableview的sectionheader悬浮代码:
#import <UIKit/UIKit.h>
@interface CSNFloatingHeaderViewFlowLayout :
UICollectionViewFlowLayout
/** zIndex for header
default value is 1024
*/
@property (nonatomic,
assign) NSInteger headerViewZIndex;
/** take over property values
@return new instance
*/
- (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)layout;
@end
-----------------------------------
#import "CSNFloatingHeaderViewFlowLayout.h"
@implementation CSNFloatingHeaderViewFlowLayout
- (void)commonInit;
{
_headerViewZIndex =
1024;
}
- (id)init
{
self = [super
init];
if (self) {
[self
commonInit];
}
return
self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super
initWithCoder:aDecoder];
if (self) {
[self
commonInit];
}
return
self;
}
- (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)layout
{
self = [self
init];
if (self) {
self.minimumLineSpacing = layout.minimumLineSpacing;
self.minimumInteritemSpacing = layout.minimumInteritemSpacing;
self.itemSize = layout.itemSize;
self.scrollDirection = layout.scrollDirection;
self.headerReferenceSize = layout.headerReferenceSize;
self.footerReferenceSize = layout.footerReferenceSize;
self.sectionInset = layout.sectionInset;
}
return
self;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *answer = [[super
layoutAttributesForElementsInRect:rect]
mutableCopy];
UICollectionView *
const collectionView = self.collectionView;
CGPoint
const contentOffset = collectionView.contentOffset;
4000
NSMutableIndexSet *missingSections = [NSMutableIndexSet
indexSet];
for (UICollectionViewLayoutAttributes *layoutAttributes
in answer) {
if (layoutAttributes.representedElementCategory ==
UICollectionElementCategoryCell) {
[missingSections addIndex:layoutAttributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *layoutAttributes
in answer) {
if ([layoutAttributes.representedElementKind
isEqualToString:UICollectionElementKindSectionHeader]&&layoutAttributes.indexPath.section
> 0) {
[missingSections removeIndex:layoutAttributes.indexPath.section];
}
}
[missingSections enumerateIndexesUsingBlock:^(NSUInteger idx,
BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath
indexPathForItem:0
inSection:idx];
UICollectionViewLayoutAttributes *layoutAttributes = [self
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:indexPath];
[answer addObject:layoutAttributes];
}];
for (UICollectionViewLayoutAttributes *layoutAttributes
in answer) {
if ([layoutAttributes.representedElementKind
isEqualToString:UICollectionElementKindSectionHeader]&&layoutAttributes.indexPath.section
> 0) {
NSInteger section = layoutAttributes.indexPath.section;
NSInteger numberOfItemsInSection = [collectionView
numberOfItemsInSection:section];
NSIndexPath *firstObjectIndexPath = [NSIndexPath
indexPathForItem:0
inSection:section];
NSIndexPath *lastObjectIndexPath = [NSIndexPath
indexPathForItem:MAX(0, (numberOfItemsInSection -
1)) inSection:section];
BOOL cellsExist;
UICollectionViewLayoutAttributes *firstObjectAttrs;
UICollectionViewLayoutAttributes *lastObjectAttrs;
if (numberOfItemsInSection >
0) { // use cell data if items exist
cellsExist = YES;
firstObjectAttrs = [self
layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
lastObjectAttrs = [self
layoutAttributesForItemAtIndexPath:lastObjectIndexPath];
} else {
// else use the header and footer
cellsExist = NO;
firstObjectAttrs = [self
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:firstObjectIndexPath];
lastObjectAttrs = [self
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
atIndexPath:lastObjectIndexPath];
}
CGFloat topHeaderHeight = (cellsExist) ?
CGRectGetHeight(layoutAttributes.frame) :
0;
CGFloat bottomHeaderHeight =
CGRectGetHeight(layoutAttributes.frame);
CGRect frameWithEdgeInsets =
UIEdgeInsetsInsetRect(layoutAttributes.frame,
collectionView.contentInset);
CGPoint origin = frameWithEdgeInsets.origin;
UIEdgeInsets sectionInset =
self.sectionInset;
origin.y =
MIN(
MAX(
contentOffset.y + collectionView.contentInset.top,
(CGRectGetMinY(firstObjectAttrs.frame) - topHeaderHeight - sectionInset.top)
),
(CGRectGetMaxY(lastObjectAttrs.frame) - bottomHeaderHeight + sectionInset.bottom)
);
layoutAttributes.zIndex =
self.headerViewZIndex;
layoutAttributes.frame = (CGRect){
.origin = origin,
.size = layoutAttributes.frame.size
};
}
}
return answer;
}
- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound
{
return
YES;
}
@end
#import <UIKit/UIKit.h>
@interface CSNFloatingHeaderViewFlowLayout :
UICollectionViewFlowLayout
/** zIndex for header
default value is 1024
*/
@property (nonatomic,
assign) NSInteger headerViewZIndex;
/** take over property values
@return new instance
*/
- (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)layout;
@end
-----------------------------------
#import "CSNFloatingHeaderViewFlowLayout.h"
@implementation CSNFloatingHeaderViewFlowLayout
- (void)commonInit;
{
_headerViewZIndex =
1024;
}
- (id)init
{
self = [super
init];
if (self) {
[self
commonInit];
}
return
self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super
initWithCoder:aDecoder];
if (self) {
[self
commonInit];
}
return
self;
}
- (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)layout
{
self = [self
init];
if (self) {
self.minimumLineSpacing = layout.minimumLineSpacing;
self.minimumInteritemSpacing = layout.minimumInteritemSpacing;
self.itemSize = layout.itemSize;
self.scrollDirection = layout.scrollDirection;
self.headerReferenceSize = layout.headerReferenceSize;
self.footerReferenceSize = layout.footerReferenceSize;
self.sectionInset = layout.sectionInset;
}
return
self;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSMutableArray *answer = [[super
layoutAttributesForElementsInRect:rect]
mutableCopy];
UICollectionView *
const collectionView = self.collectionView;
CGPoint
const contentOffset = collectionView.contentOffset;
4000
NSMutableIndexSet *missingSections = [NSMutableIndexSet
indexSet];
for (UICollectionViewLayoutAttributes *layoutAttributes
in answer) {
if (layoutAttributes.representedElementCategory ==
UICollectionElementCategoryCell) {
[missingSections addIndex:layoutAttributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *layoutAttributes
in answer) {
if ([layoutAttributes.representedElementKind
isEqualToString:UICollectionElementKindSectionHeader]&&layoutAttributes.indexPath.section
> 0) {
[missingSections removeIndex:layoutAttributes.indexPath.section];
}
}
[missingSections enumerateIndexesUsingBlock:^(NSUInteger idx,
BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath
indexPathForItem:0
inSection:idx];
UICollectionViewLayoutAttributes *layoutAttributes = [self
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:indexPath];
[answer addObject:layoutAttributes];
}];
for (UICollectionViewLayoutAttributes *layoutAttributes
in answer) {
if ([layoutAttributes.representedElementKind
isEqualToString:UICollectionElementKindSectionHeader]&&layoutAttributes.indexPath.section
> 0) {
NSInteger section = layoutAttributes.indexPath.section;
NSInteger numberOfItemsInSection = [collectionView
numberOfItemsInSection:section];
NSIndexPath *firstObjectIndexPath = [NSIndexPath
indexPathForItem:0
inSection:section];
NSIndexPath *lastObjectIndexPath = [NSIndexPath
indexPathForItem:MAX(0, (numberOfItemsInSection -
1)) inSection:section];
BOOL cellsExist;
UICollectionViewLayoutAttributes *firstObjectAttrs;
UICollectionViewLayoutAttributes *lastObjectAttrs;
if (numberOfItemsInSection >
0) { // use cell data if items exist
cellsExist = YES;
firstObjectAttrs = [self
layoutAttributesForItemAtIndexPath:firstObjectIndexPath];
lastObjectAttrs = [self
layoutAttributesForItemAtIndexPath:lastObjectIndexPath];
} else {
// else use the header and footer
cellsExist = NO;
firstObjectAttrs = [self
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader
atIndexPath:firstObjectIndexPath];
lastObjectAttrs = [self
layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter
atIndexPath:lastObjectIndexPath];
}
CGFloat topHeaderHeight = (cellsExist) ?
CGRectGetHeight(layoutAttributes.frame) :
0;
CGFloat bottomHeaderHeight =
CGRectGetHeight(layoutAttributes.frame);
CGRect frameWithEdgeInsets =
UIEdgeInsetsInsetRect(layoutAttributes.frame,
collectionView.contentInset);
CGPoint origin = frameWithEdgeInsets.origin;
UIEdgeInsets sectionInset =
self.sectionInset;
origin.y =
MIN(
MAX(
contentOffset.y + collectionView.contentInset.top,
(CGRectGetMinY(firstObjectAttrs.frame) - topHeaderHeight - sectionInset.top)
),
(CGRectGetMaxY(lastObjectAttrs.frame) - bottomHeaderHeight + sectionInset.bottom)
);
layoutAttributes.zIndex =
self.headerViewZIndex;
layoutAttributes.frame = (CGRect){
.origin = origin,
.size = layoutAttributes.frame.size
};
}
}
return answer;
}
- (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBound
{
return
YES;
}
@end
相关文章推荐
- UICollectionView 代理方法
- 研究了一下collectionView ,总结一下,防止忘记
- UIcollectionView flowlayout 流式布局
- MJRefresh
- UICollectionView
- swift UICollectionView 代码创建 和自定义Cell
- UICollectionView的基本使用
- UICollectionView 数据不够一屏 开启 上下滚动
- UICollectionView cell点击后无法回调的解决方案
- UICollectionView 与 storyboard 的一点体会
- UICollectionView 的应用 (集合视图)
- UICollectionView 详解及自定义cell
- UICollectionView 为每个section 添加headerview
- iOS 开发UICollectionView及其常用方法+实例
- iOS 手写代码UICollectionView
- UICollectionView 简单使用 代码创建
- UICollectionView 的使用
- UICollectionView介绍
- iOS之UICollectionView的使用
- UICollectionView详解