문제

나는 두 가지 섹션 UICollectionView.을 보여주고 싶은 섹션에서 헤더 UICollectionView 대만 1st section.지에서 0 번째 섹션입니다.

그래서 나를 돌아 nilviewForSupplementaryElementOfKind:방법 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: 방법은 충돌에 대한 너무 다른 사람.다른 대답이 제안을 제거하는 방법입니다.

하지만 보여주고 싶은 섹션에 헤더를 위한 특정 섹션에서만 있습니다.어떻게 그것을 달성하기 위해 돌아보기 위해 하나만 섹션?감사합니다.어떤 도움을 감사하겠습니다.

편집:

로@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;
}

나는 심지어도 설정 프레임에 대한 섹션 헤더 null 을로@san 말했다.하고 있습니다.같은 결과입니다.

도움이 되었습니까?

해결책

마침내, 나는 내 질문에 대한 답변을 발견했습니다.나는 뭔가를 놓쳤다.어쨌든 다른 동료 사용자에게 죄송합니다.

i @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 문서 링크

다른 팁

에 대한 설명서 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 상태:

이 방법으로 항상 유효한 뷰 개체입니다.당신이 원하지 않는 경우 보조기에서 특정한 경우,당신의 레이아웃을 개체를 생성하지 않아야 특성에 대한 보기입니다.또는,당신은 숨길 수 있습을 설정하여 보기 숨겨진 속성의 특성을 예로 또는 설정파 속성의 특성은 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을 반환해서는 안됩니다.

이므로 다음이 필요합니다 -

이 메소드는 항상 유효한 뷰 객체를 반환해야합니다.특정 경우에 보충 뷰를 원하지 않으면 레이아웃 개체가 해당보기의 속성을 만들지 않아야합니다.또는 해당 속성의 숨겨진 속성을 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;
}
}
.

그래서 숨기기 / 표시

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top