将属性添加到自定义 UI 视图单元格会导致应用崩溃(基于 XIB 的应用)



我有一个UICollectionViewCell的子类。现在,它的视图中只有一个标签。如果不将标签连接到文件所有者,视图控制器将正常运行。但是,当我将此标签连接到类的头文件并尝试通过其属性更改其文本时,应用程序崩溃并显示以下消息:

*** 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

请帮忙。

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

最新更新