2014-12-13 96 views
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 

请帮助。

+0

重新连接IBOutlet连接并验证其类型和名称。 – 2014-12-13 17:33:36

+0

连接到插座的XIB文件连接有问题:您可能连接到文件所有者,而不是您的自定义单元 – 2014-12-13 17:39:22

回答

0

不要将插座连接到文件的所有者(事实上,将文件的所有者作为NSObject保留)。将单元的类更改为您的自定义类,然后从单元连接到标签。

相关问题