UICollectionView 分組頭部復(fù)用重疊問題解決辦法

1 自定義分組頭部

@interfacePropertyHeader :UIView

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation PropertyHeader

- (UILabel*)titleLabel{

? ? if (!_titleLabel) {

? ? ? ? _titleLabel= [UILabelnew];

? ? ? ? _titleLabel.font= [UIFontsystemFontOfSize:14];

? ? }

? ? return _titleLabel;

}

- (instancetype)init{

? ? self= [superinit];

? ? if(self) {

? ? ? ? [selfaddSubview:self.titleLabel];

? ? ? ? [self.titleLabelmas_makeConstraints:^(MASConstraintMaker*make) {

? ? ? ? ? ? make.left.equalTo(@10);

? ? ? ? ? ? make.top.bottom.equalTo(self);

? ? ? ? ? ? make.right.equalTo(@-10);

? ? ? ? }];

? ? }

? ? return self;

}

@end

2.頭部代理方法

- (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView viewForSupplementaryElementOfKind:(NSString*)kind atIndexPath:(NSIndexPath*)indexPath {

? ? UICollectionReusableView *reusableView = nil;

? ? if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

? ? ? ? UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];

? ? ? ? for(UIView*vinview.subviews) {

? ? ? ? ? ? [vremoveFromSuperview];

? ? ? ? }

? ? ? ?PropertyHeader *header = [[PropertyHeader alloc] init];

? ? ? ? header.frame=CGRectMake(0,0,SCREEN_WIDTH,30);

? ? ? ? [view addSubview:header];

? ? ? ? [header mas_makeConstraints:^(MASConstraintMaker*make) {

? ? ? ? ? ? make.top.left.right.bottom.equalTo(@0);

? ? ? ? }];

? ? ? ? header.titleLabel.text=_dataSource[indexPath.section][@"name"];

? ? ? ? return view;

? ? }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){

? ? ? ? ? ? UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:NSStringFromClass([UICollectionReusableView class]) forIndexPath:indexPath];

? ? ? ? ? ? [view addSubview:self.footerView];

? ? ? ? ? ? return view;

? ? }

? ? return reusableView;

}

重點(diǎn)注意:

? ? ? ? for(UIView*vinview.subviews) {

? ? ? ? ? ? [v removeFromSuperview];

? ? ? ? }

如未移除,會出現(xiàn)堆疊的問題。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容