我创建了一个UICollectionView,当没有数据显示时,我遇到了这个崩溃:
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
有人知道这里有什么问题吗? 提前谢谢!
丹尼尔
修改
这是获取部分数量的代码:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
int number = 0;
if ([_dicData objectForKey:_strCurDate] ) {
number = [[_dicData valueForKey:_strCurDate] count];
}
return number;
}
* 编辑2
我已经从我的代码中移除了上面的委托方法,并且不再发生崩溃,我觉得有点连线,但我认为这是由于0返回值可能导致崩溃。
答案 0 :(得分:3)
我猜你还没有实现numberOfSections委托功能。
答案 1 :(得分:1)
实现no section of delegate。这是代码
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
return [self.searches count];
}
在该工具之后
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection: (NSInteger)section
{
NSString *searchTerm = self.searches[section];
return [self.searchResults[searchTerm] count];
}
答案 2 :(得分:1)
我实际上有一次这次崩溃不是因为我为一个部分中的一些部分或项目返回零,而是因为我正在重复使用这样的流程布局以获得多个集合视图:
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];
相反,如果我为每个UICollectionView创建一个新的流布局,我会避免这种崩溃。希望这可能有助于某人
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];
答案 3 :(得分:0)
试试这个:
- (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;
}
}
答案 4 :(得分:0)
您正在使用自定义集合布局UICollectionViewLayout
/ UICollectionViewFlowLayout
在此自定义布局内,您可以看到装饰视图。
错误意思是您不能为空的UICollectionView
您可以在自定义布局类中执行的操作是检查您是否未返回装饰视图的UICollectionViewLayoutAttributes
中的representedElementKind
。