—— Weekly technical review

We all know how to give tableView add to headerView, The implementation is relatively simple , There are many scenarios , But for giving collectionView add to header There are fewer scenes , Today's need is to give collectionView add to headerView Encountered some problems . There are two main problems :

Question one
The system I use UICollectionReusableView Then there will be a problem , stay header Every time you refresh the place, it will initialize one view
, Open many interfaces headerView They overlap , So I checked the methods of the big guys , It's all about judging this view of subviews.count
If yes 0 Then add the sub view you want to add . Yes, this solves the problem of overlap .

Question two
When you refresh , You'll find you headerView The data above is confused , This should be headerView
The problem of reuse . After reuse, the child views are also reused directly , So the data is not based on their own indexPath.section
De assignment . I haven't understood what happened for a long time , Then I customized one header, Solved the problem perfectly .

custom LHJCollectionReusableView
@interface LHJCollectionReusableView : UICollectionReusableView @end
@implementation LHJCollectionReusableView @end
collectionView register headerView
[collectionView registerClass:[MGSEpisodeViewAdHeaderView class]
collectionview Implement proxy method operations headerView
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(
UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(
NSInteger)section { return CGSizeMake(UIScreen.mainScreen.bounds.size.width, 100
); } - (UICollectionReusableView *)collectionView:(UICollectionView *)
collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(
NSIndexPath*)indexPath { if ([kind isEqualToString:
UICollectionElementKindSectionHeader]) { LHJCollectionReusableView *headerView =
[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:
NSStringFromClass(LHJCollectionReusableView.class) forIndexPath:indexPath]; [
headerView removeAllSubviews]; // headerView Add additional on view And controls return headerView; }
return nil; }
<> summary

Simple implementation collectionView of headerView, Perfectly realized my problem , If there are better friends to comment on, ha , I also hope to provide help to small partners who encounter the same problems .