今天寫項目時 用到了collectionview,但是在添加了頭視圖之后,發現按鈕一會能點一會不能點,當我把view背景色變成紅色之后,發現上面又覆蓋了一層。
FUGAI.png
最后用了暴力解決方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"forIndexPath:indexPath];
header.backgroundColor = [UIColor redColor];
for (UIView *view in header.subviews) {
[view removeFromSuperview];
}
if (indexPath.section == 0) {
self.topLabel.text = @"點我";
self.topLabel.backgroundColor = [UIColor yellowColor];
self.topLabel.textAlignment = NSTextAlignmentCenter;
self.topLabel.font = H15;
self.topLabel.textColor = [UIColor grayColor];
[header addSubview:self.topLabel];
}
return header;
}else{
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header2"forIndexPath:indexPath];
header.backgroundColor = [UIColor greenColor];
return header;
}
}
這里本來想直接return nil 但是發現return nil 之后 直接就掛了 所以又在上面注冊了第二個header視圖
[_mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
[_mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header2"];
添加頭視圖方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
}
目前能想到的方案是這樣 不知道大家有沒有更好的方案。