UICollectionView registerClass在UIViewController中不起作用(带有图片,代码和日志)

时间:2014-07-30 01:55:31

标签: ios objective-c uicollectionview

我有一个程序,我想将屏幕分成两个不同的部分(UISplitViewController在这里不适用,因为我已经有一个UINavigationController作为rootMenuController。

问题是我无法使用我的UITableView或我的UICollectionView来使用registerClass方法。对于UITableView来说,这不是问题,但它是UICollectionView所必需的。我运行模拟器来显示UICollectionView断开连接的情况。

如果不注册用于单元格重用标识符的类>

,我做错了什么
#import "SELMenuViewController.h"

@interface SELMenuViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UIButton *paymentButton;

@end

@implementation SELMenuViewController
- (IBAction)employeeSelect:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
}

- (IBAction)paymentScreen:(id)sender {
    NSLog(@"payment screen");
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:    (NSInteger)section {
    return 1;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"menuCell" forIndexPath:indexPath];

    cell.backgroundColor = [UIColor redColor];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    cell.textLabel.text = @"text";
    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.itemsOrdered = [[UITableView alloc] init];
        self.menuItems = [[UICollectionView alloc] initWithFrame:self.view.frame     collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    [self.menuItems registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"menuCell"];
    }
    return self;
}

这是错误日志...

2014-07-29 18:52:40.302 OlymPOS [2267:60b] *断言失败 - [UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:],/ SourceCache / UIKit_Sim / UIKit-2935.137 / UICollectionView。 L:3241 2014-07-29 18:52:40.306 OlymPOS [2267:60b] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使类型的视图出列:带标识符menuCell的UICollectionElementKindCell - 必须注册一个nib或标识符的类或连接故事板中的原型单元格' ***第一次抛出调用堆栈: (     0 CoreFoundation 0x00000001019a5495 exceptionPreprocess + 165     1 libobjc.A.dylib 0x000000010170499e objc_exception_throw + 43     2 CoreFoundation 0x00000001019a531a + [NSException raise:format:arguments:] + 106     3基金会0x00000001012a0f19 - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 189     4 UIKit 0x000000010083e2b7 - [UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 1324     5 OlymPOS 0x0000000100013efe - [SELMenuViewController collectionView:cellForItemAtIndexPath:] + 110     6 UIKit 0x0000000100831cae - [UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 264     7 UIKit 0x000000010083330b - [UICollectionView _updateVisibleCellsNow:] + 3581     8 UIKit 0x0000000100836ae1 - [UICollectionView layoutSubviews] + 243     9 UIKit 0x0000000100311993 - [UIView(CALayerDelegate)layoutSublayersOfLayer:] + 354     10 QuartzCore 0x0000000104475802 - [CALayer layoutSublayers] + 151     11 QuartzCore 0x000000010446a369 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 363     12 QuartzCore 0x000000010446a1ea _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24     13 QuartzCore 0x00000001043ddfb8 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 252     14 QuartzCore 0x00000001043df030 _ZN2CA11Transaction6commitEv + 394     15 QuartzCore 0x00000001043df69d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89     16 CoreFoundation 0x0000000101970dc7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23     17 CoreFoundation 0x0000000101970d37 __CFRunLoopDoObservers + 391     18 CoreFoundation 0x0000000101950522 __CFRunLoopRun + 946     19 CoreFoundation 0x000000010194fd83 CFRunLoopRunSpecific + 467     20 GraphicsServices 0x0000000104002f04 GSEventRunModal + 161     21 UIKit 0x00000001002b1e33 UIApplicationMain + 1010     22 OlymPOS 0x000000010000f663 main + 115     23 libdyld.dylib 0x00000001025235fd start + 1     24 ??? 0x0000000000000001 0x0 + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

iOS Simulator Xib image

1 个答案:

答案 0 :(得分:0)

您可以在init中创建一个集合视图,正确注册您的课程,并将其设置为您的(可能)插座。

然后,将加载视图,该视图将在以后发生而不是init,这将创建另一个集合视图并将其分配给插座。此集合视图尚未注册类,因此失败。

如果你的xib中有一个,你不清楚为什么要打扰一个集合视图,除非这是一个绝望的解决方法。

正确的做法是在viewDidLoad中注册该类,此时将存在来自nib的集合视图。