D.D.
D.D.

Reputation: 333

Crashs when no data for UICollectionView

I've created a UICollectionView, when there are no data to display, I got this crash:

2014-03-19 14:16:48.871 App[5324:90b] *** Assertion failure in -[UICollectionViewData layoutAttributesForDecorationViewOfKind:atIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UICollectionViewData.m:794
2014-03-19 14:16:54.427 App[5324:90b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for layout attributes for decoration view of kind MSCollectionElementKindCurrentTimeHorizontalGridline in section 0 when there are only 0 sections in the collection view'
*** First throw call stack:
(
0   CoreFoundation                      0x01b301e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x018af8e5 objc_exception_throw + 44
2   CoreFoundation                      0x01b30048 +[NSException raise:format:arguments:] + 136
3   Foundation                          0x000a24de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4   UIKit                               0x00b5e290 -[UICollectionViewData layoutAttributesForDecorationViewOfKind:atIndexPath:] + 228
5   UIKit                               0x00b28764 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 242
6   UIKit                               0x0e5c3344 -[UICollectionViewAccessibility(SafeCategory) _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 542
7   UIKit                               0x00b1b036 -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:] + 381
8   UIKit                               0x00b1c775 -[UICollectionView _updateVisibleCellsNow:] + 4730
9   UIKit                               0x00b2065f -[UICollectionView layoutSubviews] + 265
10  UIKit                               0x00543964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11  libobjc.A.dylib                     0x018c182b -[NSObject performSelector:withObject:] + 70
12  QuartzCore                          0x0158b45a -[CALayer layoutSublayers] + 148
13  QuartzCore                          0x0157f244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14  QuartzCore                          0x0157f0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
15  QuartzCore                          0x014e57fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
16  QuartzCore                          0x014e6b85 _ZN2CA11Transaction6commitEv + 393
17  QuartzCore                          0x015a45b0 +[CATransaction flush] + 52
18  UIKit                               0x004d29bb _UIApplicationHandleEventQueue + 13095
19  CoreFoundation                      0x01ab977f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
20  CoreFoundation                      0x01ab910b __CFRunLoopDoSources0 + 235
21  CoreFoundation                      0x01ad61ae __CFRunLoopRun + 910
22  CoreFoundation                      0x01ad59d3 CFRunLoopRunSpecific + 467
23  CoreFoundation                      0x01ad57eb CFRunLoopRunInMode + 123
24  GraphicsServices                    0x02d8c5ee GSEventRunModal + 192
25  GraphicsServices                    0x02d8c42b GSEventRun + 104
26  UIKit                               0x004d4f9b UIApplicationMain + 1225
27  ClassSchedule                       0x00002b9d main + 141
28  libdyld.dylib                       0x02a73725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Anyone knows what's wrong here? Thanks in advance!

Daniel

EDIT

here is the code to get the number of sections:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
int number = 0;
if ([_dicData objectForKey:_strCurDate]  ) {
    number = [[_dicData valueForKey:_strCurDate] count];

}

return number;
}

*EDIT 2

I've removed the above delegate method form my code, and no crashes happen any more, I feel a little wired but I think it due to the 0 return value which may cause the crash.

Upvotes: 2

Views: 7264

Answers (5)

Ayman Ibrahim
Ayman Ibrahim

Reputation: 1399

You are using a custom collection layout UICollectionViewLayout / UICollectionViewFlowLayout

Where inside this custom layout you have a decoration view.

What is the error saying is that you can not have a decoration view for an empty UICollectionView

What you can do inside you custom layout class is to check that you are not returning a UICollectionViewLayoutAttributes of representedElementKind your decoration view.

Upvotes: 1

Rohit
Rohit

Reputation: 1174

implement no of section delegate. here is the code

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView 
{
    return [self.searches count]; 
}

after that implement

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:    (NSInteger)section 
{
    NSString *searchTerm = self.searches[section];
    return [self.searchResults[searchTerm] count]; 
}

Upvotes: 1

Tea
Tea

Reputation: 306

Try this one:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (self.albums && self.albums.count > 0) {
        return self.albums.count;
    }
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.albums && self.albums.count > 0) {
        AlbumModel *album = self.albums[section];
        return album.photos.count;
    } else {
        return 0;
    }


}

Upvotes: 0

Chris Klingler
Chris Klingler

Reputation: 5304

I actually got this crash one time not because I was returning zero for a number of sections or items in a section but because I was reusing a flow layout like this for more than 1 collection view:

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
Collection1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f) collectionViewLayout:flowLayout];
[Collection1 setDataSource:self];
[Collection1 setDelegate:self];
[self.view addSubview:Collection1];

Collection2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, self.view.frame.size.height) collectionViewLayout:flowLayout];
Collection2.backgroundColor = [UIColor whiteColor];

Instead if I create a new flow layout for each UICollectionView I avoid this crash. Hopefully that might help someone

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
Collection1 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f) collectionViewLayout:flowLayout];
[Collection1 setDataSource:self];
[Collection1 setDelegate:self];
[self.view Collection1];

UICollectionViewFlowLayout *flowLayoutVert = [[UICollectionViewFlowLayout alloc] init];
[flowLayoutVert setScrollDirection:UICollectionViewScrollDirectionVertical];
Collection2 = [[UICollectionView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, self.view.frame.size.height) collectionViewLayout:flowLayoutVert];

Upvotes: 1

NeverHopeless
NeverHopeless

Reputation: 11233

I guess you haven't implemented numberOfSections delegate function.

Upvotes: 3

Related Questions