Skip to content

Commit

Permalink
新增可复制分组信息(布局还是需要重新计算)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderFM committed Jul 10, 2020
1 parent 9a635c4 commit 1e88a9a
Show file tree
Hide file tree
Showing 19 changed files with 236 additions and 61 deletions.
16 changes: 11 additions & 5 deletions Example/FMLayoutKit/FMCollViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 33,9 @@ - (void)dealloc{
}

- (void)reloadSection{
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:1 inSection:4] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
FMLayoutBaseSection *section = [self.shareSections[2] copy];
[self.collectionView insertLayoutSection:section atIndex:1];
[self.collectionView reloadData];
return;
{
FMLayoutFixedSection *section = [FMLayoutFixedSection sectionWithSectionInset:UIEdgeInsetsMake(0, 15, 15, 15) itemSpace:10 lineSpace:10 column:3];
Expand All @@ -51,7 53,10 @@ - (void)reloadSection{
section.cellElement = [FMLayoutElement elementWithViewClass:[FMCollectionCustomCell class]];
[self.collectionView insertLayoutSection:section atIndex:1];
}
[self.collectionView reloadData];
// [self.collectionView.layout invalidateLayout];
// [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:1]];

// [self.collectionView reloadData];
}

- (void)addItem{
Expand All @@ -66,7 71,7 @@ - (void)insetItem{

- (void)deleteSection{
[self.collectionView deleteLayoutSectionAt:1];
[self.collectionView reloadData];
// [self.collectionView reloadData];
}

- (void)deleteItem{
Expand All @@ -76,7 81,7 @@ - (void)deleteItem{

- (void)reloadItem{
[[self.shareSections firstObject] markChangeAt:3];
[self.collectionView reloadData];
// [self.collectionView reloadData];
}

- (void)addSection{
Expand All @@ -94,7 99,8 @@ - (void)addSection{
section.itemSize = CGSizeMake(100, 100);
section.itemDatas = [@[@"1", @"2", @"3", @"1", @"2", @"3"] mutableCopy];
section.cellElement = [FMLayoutElement elementWithViewClass:[FMCollectionCustomCell class]];
[self.shareSections addObject:section];
// [self.shareSections addObject:section];
[self.collectionView appendLayoutSections:@[section]];
}
[self.collectionView reloadData];
}
Expand Down
23 changes: 22 additions & 1 deletion FMLayoutKit/Classes/Cross/FMLayoutCrossSection.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,27 @@ @interface FMLayoutCrossSection ()

@implementation FMLayoutCrossSection

- (id)copyWithZone:(NSZone *)zone{
FMLayoutCrossSection *section = [super copyWithZone:zone];

section.cellElement = [self.cellElement copy];
section.configureCellData = self.configureCellData;

section.autoMaxSize = self.autoMaxSize;
section.size = self.size;
section.canReuseCell = self.canReuseCell;

NSMutableArray *sections = [NSMutableArray array];
for (FMLayoutBaseSection *s in self.sections) {
[sections addObject:[s copy]];
}
section.sections = sections;
section.scrollDidScroll = [self.scrollDidScroll copy];
section.configureCollectionView = [self.configureCollectionView copy];

return section;
}

- (FMLayoutDirection)crossDirection{
return self.direction == FMLayoutDirectionVertical ? FMLayoutDirectionHorizontal : FMLayoutDirectionVertical;
}
Expand Down Expand Up @@ -48,7 69,7 @@ - (instancetype)init
self = [super init];
if (self) {
self.cellElement = [FMLayoutElement elementWithViewClass:[FMLayoutCrossCell class]];
self.canReuseCell = YES;
self.canReuseCell = NO;
[self setConfigureCellData:^(FMLayoutBaseSection * _Nonnull section, UICollectionViewCell * _Nonnull cell, NSInteger item) {
FMLayoutCrossCell *hCell = (FMLayoutCrossCell *)cell;
hCell.crossSection = (FMLayoutCrossSection *)section;
Expand Down
4 changes: 2 additions & 2 deletions FMLayoutKit/Classes/Element/FMLayoutElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 11,11 @@

NS_ASSUME_NONNULL_BEGIN

@interface FMLayoutElement : NSObject
@interface FMLayoutElement : NSObject<NSCopying>

@property(nonatomic, copy)NSString *reuseIdentifier;
@property(nonatomic, assign)BOOL isNib;
@property(nonatomic, assign)Class viewClass;///视图的类
@property(nonatomic, weak)Class viewClass;///视图的类

(instancetype)elementWithViewClass:(Class)vCalss;
(instancetype)elementWithViewClass:(Class)vCalss isNib:(BOOL)isNib;
Expand Down
8 changes: 8 additions & 0 deletions FMLayoutKit/Classes/Element/FMLayoutElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 16,14 @@ @interface FMLayoutElement ()

@implementation FMLayoutElement

- (id)copyWithZone:(NSZone *)zone{
FMLayoutElement *element = [[[self class] allocWithZone:zone] init];
element.reuseIdentifier = self.reuseIdentifier;
element.isNib = self.isNib;
element.viewClass = self.viewClass;
return element;
}

(instancetype)elementWithViewClass:(Class)vCalss{
return [self elementWithViewClass:vCalss isNib:NO];
}
Expand Down
6 changes: 6 additions & 0 deletions FMLayoutKit/Classes/Element/FMLayoutFooter.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 14,10 @@ - (NSString *)elementKind{
return UICollectionElementKindSectionFooter;
}

- (id)copyWithZone:(NSZone *)zone{
FMLayoutFooter *footer = [super copyWithZone:zone];
footer.topMargin = self.topMargin;
return footer;
}

@end
15 changes: 15 additions & 0 deletions FMLayoutKit/Classes/Element/FMLayoutHeader.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 10,23 @@

@implementation FMLayoutHeader

(instancetype)elementSize:(CGFloat)size viewClass:(Class)vClass isNib:(BOOL)isNib reuseIdentifier:(NSString *)reuseIdentifier{
FMLayoutHeader *header = [super elementSize:size viewClass:vClass isNib:isNib reuseIdentifier:reuseIdentifier];
return header;
}

- (NSString *)elementKind{
return UICollectionElementKindSectionHeader;
}

- (id)copyWithZone:(NSZone *)zone{
FMLayoutHeader *header = [super copyWithZone:zone];
header.lastMargin = self.lastMargin;
header.type = self.type;
header.suspensionTopMargin = self.suspensionTopMargin;
header.isStickTop = self.isStickTop;
header.minSize = self.minSize;
return header;
}

@end
9 changes: 9 additions & 0 deletions FMLayoutKit/Classes/Element/FMSupplementary.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,15 @@ @interface FMSupplementary ()

@implementation FMSupplementary

- (id)copyWithZone:(NSZone *)zone{
FMSupplementary *supp = [super copyWithZone:zone];
supp.size = self.size;
supp.zIndex = self.zIndex;
supp.autoHeight = self.autoHeight;
supp.configureDataAutoHeight = [self.configureDataAutoHeight copy];
return supp;
}

(instancetype)elementSize:(CGFloat)size viewClass:(Class)vClass{
return [self elementSize:size viewClass:vClass isNib:NO];
}
Expand Down
9 changes: 9 additions & 0 deletions FMLayoutKit/Classes/FMCollectionLayoutAttributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,9 @@
@implementation FMCollectionLayoutAttributes

(instancetype)headerAttributesWithSection:(FMLayoutBaseSection *)section{
if (!section.header) {
return nil;
}
FMCollectionLayoutAttributes *header = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:section.indexPath];
header.direction = section.direction;
if (header.direction == FMLayoutDirectionVertical) {
Expand Down Expand Up @@ -197,6 200,9 @@ (instancetype)suspensionShowHeaderAttributes:(FMLayoutBaseSection *)section{


(instancetype)footerAttributesWithSection:(FMLayoutBaseSection *)section{
if (!section.footer) {
return nil;
}
FMCollectionLayoutAttributes *footer = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:section.indexPath];
footer.direction = section.direction;
if (footer.direction == FMLayoutDirectionVertical) {
Expand All @@ -221,6 227,9 @@ - (instancetype)updateFooterAttributesWithSection:(FMLayoutBaseSection *)section
}

(instancetype)bgAttributesWithSection:(FMLayoutBaseSection *)section{
if (!section.background) {
return nil;
}
FMCollectionLayoutAttributes *bgAttr = [self layoutAttributesForSupplementaryViewOfKind:section.background.elementKind withIndexPath:section.indexPath];
bgAttr.direction = section.direction;
if (bgAttr.direction == FMLayoutDirectionVertical) {
Expand Down
5 changes: 0 additions & 5 deletions FMLayoutKit/Classes/FMLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 30,6 @@ - (void)prepareLayout{
[self handleSections];
}

- (void)invalidateLayout{

[super invalidateLayout];
}

- (void)setSections:(NSMutableArray<FMLayoutBaseSection *> *)sections{

if (_sections == sections) {
Expand Down
6 changes: 5 additions & 1 deletion FMLayoutKit/Classes/FMLayoutView.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 22,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface FMLayoutView : UICollectionView
@property(nonatomic, weak)id<FMCollectionLayoutViewConfigurationDelegate> configuration;
@property(nonatomic, weak)FMLayout *layout;
///重写了set get 目标指向->layout.sections
@property(nonatomic)NSMutableArray<FMLayoutBaseSection *> *sections;
@property(nonatomic, assign)BOOL reloadOlnyChanged;
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout NS_UNAVAILABLE;
- (instancetype)initHorizontal;
Expand All @@ -38,8 40,10 @@ NS_ASSUME_NONNULL_BEGIN

/// 交换分组
- (void)exchangeLayoutSection:(NSUInteger)index to:(NSUInteger)to;

/// 循环遍历出更改过的sections 如果有过增删分组 请使用原有的reloadData方法
- (void)reloadChangedSectionsData;
///获取到滚动到indexPath位置偏移量 如果获取到的不准确 请先调用layoutIfNeeded方法
- (CGPoint)contentOffsetScrollToIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition;

@end

Expand Down
Loading

0 comments on commit 1e88a9a

Please sign in to comment.