developer tip

UICollectionReusableView 메서드가 호출되지 않음

copycodes 2020. 11. 9. 08:14
반응형

UICollectionReusableView 메서드가 호출되지 않음


의 섹션에 UICollectionView이미지가있는 머리글이 있어야합니다.

나는 다음 단계를 따랐다.

  • 스토리 보드에서 헤더를 액세서리로 할당했습니다. UICollectionView
  • 식별자를 주었다
  • UICollectionReusableView그것에 대한 하위 클래스를 만들었습니다.
  • 스토리 보드의 클래스에 사용자 지정 클래스를 할당했습니다.
  • ImageView헤더 액세서리에 넣어
  • 파일 ImageView의 사용자 정의 클래스에 대한 콘센트를 만들었습니다..h
  • 에서 다음을 구현했습니다 viewDidLoad.

 

[self.collectionView registerClass:[ScheduleHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier];

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader)
    {
        ScheduleHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

        headerView.headerImageView.image = [UIImage imageNamed:@"blah.png"];

        reusableview = headerView;
    }

    return reusableview;
}

모든 셀과 해당 섹션을 볼 수 있기 때문에 데이터 소스 및 대리자 메서드가 작동하고 있음을 알고 있습니다. 그러나 내 헤더가 없습니다. 위의 메서드에 중단 점을 입력했는데 호출되지 않습니다.

내가 도대체 ​​뭘 잘못하고있는 겁니까?


헤더에 0이 아닌 크기를 지정해야하거나 collectionView:viewForSupplementaryElementOfKind:atIndexPath호출되지 않은 것 같습니다 . 따라서 headerReferenceSize다음과 같이 흐름 레이아웃 속성을 설정합니다 .

flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.frame.size.width, 100.f);

또는 collectionView:layout:referenceSizeForHeaderInSection섹션별로 크기를 변경하려는 경우 구현 하십시오.


당신은 질문에 대답했지만 말로 더 잘 이해합니다.

메서드를 호출하려면 다음 메서드를 추가하십시오.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);
}

... 그리고 거기에서 가십시오.


Swift 4 Xcode 9.1 베타 2

@objc 추가

@objc func collectionView(_ collectionView: UICollectionView, layout  collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize{
    let size = CGSize(width: 400, height: 50)
    return size
}

크레딧 : https://medium.com/@zonble/your-delegation-methods-might-not-be-called-in-swift-3-c6065ed7b4cd


현재 인터페이스에 UICollectionViewDelegateFlowLayout을 추가 했습니까? 이것은 나를 위해 일했습니다.

@interface MyViewController () <UICollectionViewDelegateFlowLayout>

빠른:

class MyViewController: UICollectionViewDelegateFlowLayout {


Swift 3.0 (xcode 8.2.1)

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    return CGSize(width:collectionView.frame.size.width, height:30.0)
}

빠른 버전이 있습니다.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    let size = CGSize(width: 400, height: 50)
    return size
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    let size = CGSize(width: 400, height: 50)
    return size
}

XCode (버전 7.3.1)는 자동 완성에서 이러한 메서드를 제안하지 않습니다!

헤더 만 원하는 경우 헤더 메소드를 유지하십시오.


My problem was that in swift 3, the name of the viewForSupplementaryElementOfKind function has changed slightly from any posts that were on stack overflow. Hence, it was never getting called. Make sure that, if you're in swift 3, you're delegate function matches:

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {}

I have the same issue. I have added referenceSizeForFooterInSection but then also viewForSupplementaryElementOfKind not getting called. I have added this code in viewDidLoad() and it worked for me:

if let flowLayout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.sectionFootersPinToVisibleBounds = true   
}

For me, in Swift 4.2, func collectionView(collectionView:kind:indexPath:) -> UICollectionReusableView was never being called. This was for a collection view in a UIViewController. I noticed the existing collection view functions were qualified with @objc, and that the UICollectionView had not adopted the UICollectionViewDataSource and UICollectionViewDelegate protocols. As soon as I adopted the protocols, I got errors that the collection view functions did not match the protocol. I corrected the function syntax, removed the qualifiers, and the section headers started working.

참고URL : https://stackoverflow.com/questions/18173191/uicollectionreusableview-method-not-being-called

반응형