セクションヘッダーがUICollectionViewに隠されている場合、空のスペースを削除します
-
22-12-2019 - |
質問
私は2つのセクションを持っています UICollectionView
.私はセクションヘッダーを表示したい UICollectionView
第1節のみ。第0節ではありません。
だから私は戻ろうとしました nil
で viewForSupplementaryElementOfKind
:のための方法 section == 0
のビューを返します。 section == 1
.
それはクラッシュし、以下のエラーを示しています:
Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes]:
ここでは、補足ビューのための私のコードです。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *sectionHeader = nil;
if (kind == UICollectionElementKindSectionHeader && indexPath.section == 1) {
sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EventSectionHeader" forIndexPath:indexPath];
sectionHeader.layer.borderWidth = .5f;
sectionHeader.layer.borderColor = [UIColor colorWithRed:221.0 / 255.0 green:223.0 / 255.0 blue:220.0 / 255.0 alpha:1.0].CGColor;
}
return sectionHeader;
}
私はnilを返すことを発見しました viewForSupplementaryElementOfKind:
メソッドも他の人のためにクラッシュします。その方法を削除することを提案する他の答え。
しかし、特定のセクションのセクションヘッダーのみを表示したいと思います。どのように1つのセクションだけのビューを返すことを達成するには?ありがとう!.任意の助けがいただければ幸いです。
編集:
@Sanが言ったように、セクションヘッダーを非表示にするコードを更新しました。それは動作します。ヘッダーを非表示にします。しかし、私はまだセクションヘッダーの代わりに空のスペースを見ています。期待される結果は、セクションヘッダーが隠されている場合、セクションヘッダー用のスペースがないことです。
更新されたコード:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *sectionHeader = nil;
if (kind == UICollectionElementKindSectionHeader) {
sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EventSectionHeader" forIndexPath:indexPath];
sectionHeader.layer.borderWidth = .5f;
sectionHeader.layer.borderColor = [UIColor colorWithRed:221.0 / 255.0 green:223.0 / 255.0 blue:220.0 / 255.0 alpha:1.0].CGColor;
if (indexPath.section == 0) {
sectionHeader.hidden = YES;
}else {
sectionHeader.hidden = NO;
}
}
return sectionHeader;
}
@Sanが言ったように、sectionHeaderのフレームを設定しようとしました。しかし、運はありません。同じ結果。
解決
最後に、私は私の質問に対する答えを見つけました。私は何かを逃しました。とにかく他の仲間のユーザーのために申し訳ありません。
@Sanが言ったように、私は今まで以下の方法の中でヘッダーの高さと幅を設定しました。
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
ただし、補足ビューのフレームサイズを設定するのは正しい方法ではありません。後で、flowlayout内に別のメソッドが見つかりました。
これは本当に私のためにうまくいきます:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return CGSizeZero;
}else {
return CGSizeMake(CGRectGetWidth(collectionView.bounds), 135);
}
}
更新: 誰かがコメントで私のスキルについて質問したので、appleを添付してください ドキュメントリンク 上記のメソッドでCGSizeZeroを返すため。
他のヒント
のためのドキュメント collectionView:viewForSupplementaryElementOfKind:atIndexPath:
州:
このメソッドは、常に有効なviewオブジェクトを返す必要があります。特定のケースで補助ビューを使用しない場合は、レイアウトオブジェクトでそのビューの属性を作成しないでください。または、対応する属性のhiddenプロパティをYESに設定するか、属性のalphaプロパティを0に設定することで、ビューを非表示にすることもできます。フローレイアウトでヘッダービューとフッタービューを非表示にするには、これらのビューの幅と高さを0に設定することもできます。
すでに高さをゼロに設定し、ビューを非表示に設定しようとしていることを考えると、サブクラス化する必要があります UICollectionViewFlowLayout
と実装 layoutAttributesForSupplementaryViewOfKind:atIndexPath:
(すでに行っているように)indexPathをチェックして返します nil
その特定の補助ビューのレイアウト属性を必要としない場合。
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if([indexPath section] == 0)
{
return nil;
}
return [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
}
ドキュメントは明らかに言う -
戻り値
構成された補助ビューオブジェクト。このメソッドからnilを返すことはできません。
だからあなたは従う必要があります -
このメソッドは、常に有効なviewオブジェクトを返す必要があります。特定のケースで補助ビューを使用しない場合は、レイアウトオブジェクトでそのビューの属性を作成しないでください。または、対応する属性のhiddenプロパティをYESに設定するか、属性のalphaプロパティを0に設定することで、ビューを非表示にすることもできます。フローレイアウトでヘッダービューとフッタービューを非表示にするには、これらのビューの幅と高さを0に設定することもできます。
あなたのコードに来ると、以下のスニペットがあなたのために働くはずです:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *sectionHeader = nil;
if (kind == UICollectionElementKindSectionHeader) {
sectionHeader = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EventSectionHeader" forIndexPath:indexPath];
if(indexPath.section == 1)
{
sectionHeader.layer.borderWidth = .5f;
sectionHeader.layer.borderColor = [UIColor colorWithRed:221.0 / 255.0 green:223.0 / 255.0 blue:220.0 / 255.0 alpha:1.0].CGColor;
}
else
{
sectionHeader.frame = CGRectZero;
sectionHeader.hidden = YES;
}
}
return sectionHeader;
}
私の場合、私はセクションに挿入を与えたので、それは私に空のスペースを与えていました したがって、次の方法を実装している場合は、以下の方法で実行してください
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
if <condition for which you want to hide section>{
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}else{
return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
}
再利用可能なヘッダーセクションを非表示/表示するには、次のようにします UICollectionViewDelegateFlowLayout
デリゲートと以下のコードを使用する
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if (self.isForSearch) { //---> for hiding
return CGSizeMake(0,0);
}
else{//---> for showing
return ((UICollectionViewFlowLayout*)self.collectionChoosePlanView.collectionViewLayout).headerReferenceSize;
}
}
だからあなたはそれを非表示/表示することができます