0
我有UICollectionViewCell的子類。目前它只有一個標籤。沒有將標籤連接到文件所有者,視圖控制器運行正常。然而,當我這個標籤連接到班級的頭文件,並試圖改變它的文本通過它的屬性,應用程序崩潰與此消息:將屬性添加到自定義UICollectionViewCell導致應用程序崩潰(基於XIB的應用程序)
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7ff543180040> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cellLabel.'
First throw call stack:
(
0 CoreFoundation 0x0000000106744f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000105fefbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000106744b79 -[NSException raise] + 9
3 Foundation 0x0000000105b8c7b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010668ee80 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x00000001072a7c7d -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x000000010761cf05 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 588
7 MyApp 0x00000001056853e1 -[ViewController collectionView:cellForItemAtIndexPath:] + 113
8 UIKit 0x000000010761041b -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 244
9 UIKit 0x0000000107611b54 -[UICollectionView _updateVisibleCellsNow:] + 3445
10 UIKit 0x0000000107615801 -[UICollectionView layoutSubviews] + 243
11 UIKit 0x000000010705b973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
12 QuartzCore 0x0000000106e6dde8 -[CALayer layoutSublayers] + 150
13 QuartzCore 0x0000000106e62a0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x0000000106e6287e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
15 QuartzCore 0x0000000106dd063e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
16 QuartzCore 0x0000000106dd174a _ZN2CA11Transaction6commitEv + 390
17 QuartzCore 0x0000000106dd1db5 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
18 CoreFoundation 0x0000000106679dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
19 CoreFoundation 0x0000000106679d20 __CFRunLoopDoObservers + 368
20 CoreFoundation 0x000000010666fb53 __CFRunLoopRun + 1123
21 CoreFoundation 0x000000010666f486 CFRunLoopRunSpecific + 470
22 GraphicsServices 0x00000001098029f0 GSEventRunModal + 161
23 UIKit 0x0000000106fe2420 UIApplicationMain + 1282
24 MyApp 0x0000000105696083 main + 115
25 libdyld.dylib 0x0000000109325145 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
CustomCollectionViewCell.h
@interface CustomCollectionViewCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UILabel *cellLabel;
@end
CustomCollectionViewCell.m
#import "CustomCollectionViewCell.h"
@implementation CustomCollectionViewCell
- (void)awakeFromNib {
}
@end
ViewController.h
@interface CalculatorViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionsView;
@end
ViewController.m
#import "ViewController.h"
#import "CustomCollectionViewCell.h"
@interface ViewController() {
NSMutableArray *sourceArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
sourceArray = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
[self.drinksCollectionView registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"Cell"];
}
#pragma mark - Collection view datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return sourceArray.count;
}
- (CustomCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CustomCollectionViewCell *cell = (CustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.cellLabel.text = sourceArray[indexPath.row];
return cell;
}
@end
請幫助。
重新連接IBOutlet連接並驗證其類型和名稱。 – 2014-12-13 17:33:36
連接到插座的XIB文件連接有問題:您可能連接到文件所有者,而不是您的自定義單元 – 2014-12-13 17:39:22