我的collectionView有问题,在iOS 6中,项目运行良好,但当我将部署目标更改为iOS 7时,项目不再有效。
**Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:cellForItemAtIndexPath: (<NSIndexPath: 0x8daa110> {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableCellWithReuseIdentifier:forIndexPath: or is nil (<CustomCollecionCell: 0x8dab520; baseClass = UICollectionViewCell; frame = (0 0; 300 190); clipsToBounds = YES; opaque = NO; autoresize = RM+TM; layer = <CALayer: 0x8daad00>>)'
*** First throw call stack:
(
0 CoreFoundation 0x029405e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01de88b6 objc_exception_throw + 44
2 CoreFoundation 0x02940448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x017bbfee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x00f5bd81 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 698
5 UIKit 0x00f6f19f __51-[UICollectionView _viewAnimationsForCurrentUpdate]_block_invoke1144 + 521
6 UIKit 0x00f6c793 -[UICollectionView _viewAnimationsForCurrentUpdate] + 2901
7 UIKit 0x00f6ff9c -[UICollectionView _updateWithItems:] + 1635
8 UIKit 0x00f6b978 -[UICollectionView _endItemAnimations] + 14317
9 UIKit 0x00f67f95 -[UICollectionView _updateRowsAtIndexPaths:updateAction:] + 361
10 UIKit 0x00f67fdc -[UICollectionView insertItemsAtIndexPaths:] + 48
11 Home Automation 0x0004d541 -[MainDashboardViewController stopDragging:] + 6209
12 Home Automation 0x00049117 -[MainDashboardViewController handlePanning:] + 215
13 UIKit 0x00cc1e8c _UIGestureRecognizerSendActions + 230
14 UIKit 0x00cc0b00 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
15 UIKit 0x00cc256d -[UIGestureRecognizer _delayedUpdateGesture] + 60
16 UIKit 0x00cc5acd ___UIGestureRecognizerUpdate_block_invoke + 57
17 UIKit 0x00cc5a4e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
18 UIKit 0x00cbc148 _UIGestureRecognizerUpdate + 199
19 UIKit 0x0098819a -[UIWindow _sendGesturesForEvent:] + 1291
20 UIKit 0x009890ba -[UIWindow sendEvent:] + 1030
21 UIKit 0x0095ce86 -[UIApplication sendEvent:] + 242
22 UIKit 0x0094718f _UIApplicationHandleEventQueue + 11421
23 CoreFoundation 0x028c983f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
24 CoreFoundation 0x028c91cb __CFRunLoopDoSources0 + 235
25 CoreFoundation 0x028e629e __CFRunLoopRun + 910
26 CoreFoundation 0x028e5ac3 CFRunLoopRunSpecific + 467
27 CoreFoundation 0x028e58db CFRunLoopRunInMode + 123
28 GraphicsServices 0x032d69e2 GSEventRunModal + 192
29 GraphicsServices 0x032d6809 GSEventRun + 104
30 UIKit 0x00949d3b UIApplicationMain + 1225
31 Home Automation 0x0000295d main + 141
32 libdyld.dylib 0x02534725 start + 0
)
libc++abi.dylib: terminating with uncaught exception of type NSException
--------------------------------------------------------------------------------------**
注意:我有两个带有不同customCell类的collectionView。
答案 0 :(得分:3)
从iOS 7开始,您需要为您使用的每个collectionviewcell注册一个nib / class,并在
中调用dequeue方法- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
由您的UICollectionViewDataSource实现。
所以首先一次调用以下方法之一(例如在viewDidLoad中):
- (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
然后使用
- (id)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath*)indexPath;
在cellForItemAtIndexPath中实例化实际的单元格。