因為項目需求奠货,需要實現(xiàn)多區(qū)標(biāo)簽選擇,標(biāo)簽的長度還是不固定的座掘,網(wǎng)上沒有找到合適的递惋,所以自己研究了下自定義Layout,實現(xiàn)了多行分區(qū)不定寬高的CustomLayout溢陪,一下是代碼:
.h文件代碼
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@protocol SSLCollectionViewLayoutDelegate <NSObject>
@optional;
//如果注冊區(qū)頭萍虽,則必須實現(xiàn)改代理方法
-(CGFloat)heightOfSectionHeaderForIndexPath:(NSIndexPath *)indexPath;
//如果注冊區(qū)頭,則必須實現(xiàn)改代理方法
-(CGFloat)heightOfSectionFooterForIndexPath:(NSIndexPath *)indexPath;
//單元格返回寬度
- (CGFloat)collectionView:(UICollectionView *)collectionView wideForItemAtIndexPath:(NSIndexPath *)indexPath;
@end
@interface SSLCollectionViewLayout : UICollectionViewLayout
//item 的高度
@property (nonatomic,assign) CGFloat itemHeight ;
//item左右的間距
@property (nonatomic,assign) CGFloat itemLRSpace;
//區(qū)邊距
@property (nonatomic, assign) UIEdgeInsets sectionEdge;
@property (nonatomic, assign) CGFloat collectionViewWidth;
//item上下的間距
@property (nonatomic,assign) CGFloat itemHVSpace;
@property (nonatomic, assign) id<SSLCollectionViewLayoutDelegate>delegate;
@end
NS_ASSUME_NONNULL_END
.m文件代碼
#import "SSLCollectionViewLayout.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define kBaseLine(a) (CGFloat)a * SCREEN_WIDTH / 375.0
@interface SSLCollectionViewLayout ()
@property (nonatomic, assign) CGFloat totalHeight; ///總高度
@property (nonatomic, assign) CGFloat itemX;//單元格的x
@property (nonatomic, strong) NSMutableArray *attrsArr; /// item布局屬性數(shù)組
@property (nonatomic, assign) NSInteger currentSection;
@end
@implementation SSLCollectionViewLayout
-(void)prepareLayout {
[super prepareLayout];
self.totalHeight = 0;
NSMutableArray *attributesArr = [NSMutableArray array];
NSInteger sectionCount = [self.collectionView numberOfSections];
for (int i = 0; i < sectionCount; i++) {
NSIndexPath *indexP = [NSIndexPath indexPathWithIndex:i];
/// header布局
if (self.delegate && [self.delegate respondsToSelector:@selector(heightOfSectionHeaderForIndexPath:)])
{
UICollectionViewLayoutAttributes *attr = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexP];
[attributesArr addObject:attr];
}
/// item布局
if (self.delegate && [self.delegate respondsToSelector:@selector(collectionView: wideForItemAtIndexPath:)])
{
NSInteger itemCount = [self.collectionView numberOfItemsInSection:i];
for (int j = 0; j < itemCount; j++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
[attributesArr addObject:attrs];
}
}
/// footer布局
if (self.delegate && [self.delegate respondsToSelector:@selector(heightOfSectionFooterForIndexPath:)])
{
UICollectionViewLayoutAttributes *attr1 = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:indexP];
[attributesArr addObject:attr1];
}
}
self.attrsArr = [NSMutableArray arrayWithArray:attributesArr];
}
/// contentSize
-(CGSize)collectionViewContentSize {
return CGSizeMake(self.collectionView.bounds.size.width, self.totalHeight + self.sectionEdge.bottom);
}
-(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *layoutAttrs = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
CGFloat height = 0;
self.itemX = self.sectionEdge.left;
if (elementKind == UICollectionElementKindSectionHeader) {
if (layoutAttrs.indexPath.section >0)
{
self.totalHeight += self.itemHeight;
self.totalHeight = self.totalHeight + self.sectionEdge.bottom;
}
CGFloat itemY = self.totalHeight;
if (_delegate != nil && [_delegate respondsToSelector:@selector(heightOfSectionHeaderForIndexPath:)]) {
height = [_delegate heightOfSectionHeaderForIndexPath:indexPath];
self.totalHeight += height;
layoutAttrs.frame = CGRectMake(0, itemY, self.collectionView.frame.size.width, height);
}
self.totalHeight = self.totalHeight + self.sectionEdge.top;
} else {
CGFloat itemY = self.totalHeight;
if (_delegate != nil && [_delegate respondsToSelector:@selector(heightOfSectionFooterForIndexPath:)]) {
height = [_delegate heightOfSectionFooterForIndexPath:indexPath];
self.totalHeight += height;
layoutAttrs.frame = CGRectMake(0, itemY, self.collectionView.frame.size.width, height);
}
}
return layoutAttrs;
}
-(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
return self.attrsArr;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
CGFloat W = 30+ arc4random_uniform(50);
if ([self.delegate respondsToSelector:@selector(collectionView:wideForItemAtIndexPath:)])
{
W = [self.delegate collectionView:self.collectionView wideForItemAtIndexPath:indexPath];
}
if (self.itemX + W > self.collectionView.frame.size.width - self.sectionEdge.left - self.sectionEdge.right)
{
self.totalHeight = self.totalHeight + self.itemHeight + self.itemHVSpace;
self.itemX = self.sectionEdge.left;
}
CGFloat itemY = self.totalHeight;
layoutAttributes.frame = CGRectMake(self.itemX, itemY , W, self.itemHeight);
self.itemX = self.itemX + W + self.itemLRSpace;
return layoutAttributes;
}
@end
有出現(xiàn)問題的請聯(lián)系我形真,歡迎關(guān)注杉编、點贊、溝通