I am implementing a header for my UICollectionView right now, and I am struggling right now since the header is called only once on the first cell.
This is what I've done - I declared UICollectionViewFlowLayout to programmatically make a UICollectionView - in this case, pickView. And then I added two xib files - one for cell, and the other one for header. I have registered those files into collection view like this:
UICollectionViewFlowLayout *layout2 = [[UICollectionViewFlowLayout alloc] init];
self.pickView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, pointView_y, pointView_width, pointView_length) collectionViewLayout:layout2];
self.pickView.backgroundColor = [UIColor whiteColor];
[self.pickView registerNib:[UINib nibWithNibName:@"TodayCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"TodayCollectionViewCell"];
[self.pickView registerNib:[UINib nibWithNibName:@"PickCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PickCollectionReusableView"];
//layout2.headerReferenceSize = CGSizeMake(_screenWidth, 50);
[self.pickView setDataSource:self];
[self.pickView setDelegate:self];
[self.view addSubview:self.pickView];
And then I declared the size of header view like this:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake([Util screenWidth], 50);
}
And finally I called viewForSupplementaryElementOfKind
to call header files like this:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
PickCollectionReusableView *reusableview = (PickCollectionReusableView*) [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PickCollectionReusableView" forIndexPath:indexPath];
PickCellVariable *buf = (PickCellVariable*) _pickArray[indexPath.row];
NSLog(@"buf id = %@, SECTION = %lu", buf.itemID, (long)indexPath.row);
if(buf != nil) {
//reusableview.thumbPic = ;
[reusableview.brandName setTitle:[NSString stringWithFormat:@"%lu", (long)indexPath.row] forState:UIControlStateNormal];
[[reusableview brandName] setBrandName:buf.brandname];
[[reusableview brandName] addTarget:self action:@selector(brandClick:) forControlEvents:UIControlEventTouchUpInside];
[[reusableview settingButton] addTarget:self action:@selector(setting:) forControlEvents:UIControlEventTouchUpInside];
return reusableview;
}
else {
[[reusableview brandName] setBrandName:@""];
return reusableview;
}
}
I put the breakpoint and added NSLog line to make sure that viewForSupplementaryElementOfKind
is only called once, in the very first cell.
I've tried to find cases like mine, but most of the issues regarding the UICollectionView header is when they registered class instead of registering xib file, or they forgot to set the size of header view. Mine is only called for once so this is not about overlap as well. My collection view is programmatically made so I do not redefine anything right now.
What could be the issue? Please, anything can help in here.
Thank you.
答案 0 :(得分:1)
Ok, so my problem was that I called my data on row
basis. However, in order to make header to appear on every cell, you have to call your data in section
basis.
I hope no one is struggling anymore like me in terms of this issue.