본문 바로가기
Xcode/ERROR

ERROR - Collection View의 HeaderView가 Nil 일 때 생기는 오류

by 후르륵짭짭 2020. 12. 19.
728x90
반응형

안녕하세요 후르륵짭짭 입니다.

이번에는 Collection view를 사용 할때, 오류가 발생하면 대처하는 방법에 대해 알아보도록 하겠습니다.

2020-12-19 22:10:52.312587+0900 Buzzni[73812:1853291] *** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], UICollectionView.m:2758
2020-12-19 22:10:52.357188+0900 Buzzni[73812:1853291] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,<NSIndexPath: 0xa651f5e7450eba11> {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (<UICollectionReusableView: 0x7f8cf7c49aa0; frame = (0 0; 0 0); layer = <CALayer: 0x6000037304c0>>)'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff20420af6 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007fff20177e78 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff2042091f +[NSException raise:format:] + 0
	3   Foundation                          0x00007fff2077056a -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
	4   UIKitCore                           0x00007fff23d72051 -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] + 687
	5   UIKitCore                           0x00007fff23d76d9d -[UICollectionView _updateVisibleCellsNow:] + 7125
	6   UIKitCore                           0x00007fff23d7bc48 -[UICollectionView layoutSubviews] + 351
	7   UIKitCore                           0x00007fff24bf25b8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2924
	8   QuartzCore                          0x00007fff27aa2c33 -[CALayer layoutSublayers] + 258
	9   QuartzCore                          0x00007fff27aa91a5 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 575
	10  QuartzCore                          0x00007fff27ab4f47 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 65
	11  QuartzCore                          0x00007fff279f4408 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 496
	12  QuartzCore                          0x00007fff27a2b1ef _ZN2CA11Transaction6commitEv + 783
	13  UIKitCore                           0x00007fff246c140f _afterCACommitHandler + 164
	14  CoreFoundation                      0x00007fff2038e1f8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
	15  CoreFoundation                      0x00007fff20388a77 __CFRunLoopDoObservers + 547
	16  CoreFoundation                      0x00007fff2038901a __CFRunLoopRun + 1113
	17  CoreFoundation                      0x00007fff203886d6 CFRunLoopRunSpecific + 567
	18  GraphicsServices                    0x00007fff2bededb3 GSEventRunModal + 139
	19  UIKitCore                           0x00007fff24690e0b -[UIApplication _run] + 912
	20  UIKitCore                           0x00007fff24695cbc UIApplicationMain + 101
	21  libswiftUIKit.dylib                 0x00007fff54d1e5f2 $s5UIKit17UIApplicationMainys5Int32VAD_SpySpys4Int8VGGSgSSSgAJtF + 98
	22  Buzzni                              0x00000001078c0c7a $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 122
	23  Buzzni                              0x00000001078c0bee $s6Buzzni11AppDelegateC5$mainyyFZ + 46
	24  Buzzni                              0x00000001078c0cc9 main + 41
	25  libdyld.dylib                       0x00007fff202593e9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,<NSIndexPath: 0xa651f5e7450eba11> {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (<UICollectionReusableView: 0x7f8cf7c49aa0; frame = (0 0; 0 0); layer = <CALayer: 0x6000037304c0>>)'
terminating with uncaught exception of type NSException

대략 적으로 오류는 이렇게 발생합니다,,,,

처음엔 이게 머꼬,,, 했지만 ㅠㅠ

문제는 제 코드에서 발생했습니다.

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

guard let headerViewItem = headerViewItem else {
 print("no Header View")
return UICollectionReusableView()}

.... 생략 }

이렇게 해더뷰를 생성하는 곳에 아이템이 없을 경우에 UiCollectionReusableView를 반환하도록 되어 있습니다.

하지만 만약 UICollectionView의 크기가 동적인 값이 아니라 고정값을 가지고 있다면 위와 같은 오류가 발생합니다.

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: liveView.bounds.width , height: 200)
    }

이렇게 고정값을 가지게 된다면 오류가 발생합니다.

따라서 이러한 문제를 해결하기 위해서는 

 guard let header = liveCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "LiveCollectionHeaderView", for: indexPath) as? LiveCollectionHeaderView else {return UICollectionReusableView()}
            
guard let headerViewItem = headerViewItem else {
	print("no Header View")
return header}

이렇게 우리가 원래 사용하려던 header를 반환하면 됩니다!

 

 

참고 사이트 : 

stackoverflow.com/questions/44843008/what-to-return-from-collectionview-viewforsupplementaryelementofkindat-when

 

What to return from collectionView(_:viewForSupplementaryElementOfKind:at:) when you want to return nothing?

I have a UICollectionView that has section headers, but not section footers. As such, I don't have a defined footer view. Apple's documentation states that You must not return nil from this method.

stackoverflow.com

 

728x90
반응형

댓글