Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView注册表类在UIViewController中不工作(带有pics、代码和日志)_Ios_Objective C_Uicollectionview - Fatal编程技术网

Ios UICollectionView注册表类在UIViewController中不工作(带有pics、代码和日志)

Ios UICollectionView注册表类在UIViewController中不工作(带有pics、代码和日志),ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我有一个程序,我想把屏幕分成两个不同的部分。UISplitViewController在这里不适用,因为我已经有一个UINavigationController作为rootMenuController 问题是我无法让UITableView或UICollectionView使用registerClass方法。对于UITableView,这不是问题,但是UICollectionView需要它。我运行模拟器来显示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\u dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:,/SourceCache/UIKit\u Sim/UIKit-2935.137/UICollectionView.m:3241中断言失败 2014-07-29 18:52:40.306 OlymPOS[2267:60b]*由于未捕获的异常“NSInternalInconsistenceException”而终止应用程序,原因:“无法将某类视图出列:UICollectionElementKindCell带有标识符menuCell-必须为标识符注册nib或类,或在情节提要中连接原型单元” ***第一次抛出调用堆栈: 0 CoreFoundation 0x00000001019a5495异常预处理+165 1 libobjc.A.dylib 0x000000010170499e objc_异常_抛出+43 2 CoreFoundation 0x00000001019a531a+[N异常提升:格式:参数:][106 3基金会0x000 000 01012A0F19-[ NSRealStutsHuffer-HooRealRuleIn方法:对象:文件:文件号:描述:] + 189 4 UIKit 0x000000010083e2b7-[UICollectionView\u dequeueReusableViewOfKind:带标识符:forIndexPath:viewCategory:+1324 5奥林巴斯0x0000000100013efe-[SELMenuViewController集合视图:cellForItemAtIndexPath:+110 6 UIKit 0x0000000100831cae-[UICollectionView\u createPreparedCellForItemAtIndexPath:WithLayoutAttribute:applyAttributes:+264 7 UIKit 0x000000010083330b-[UICollectionView\u updateVisibleCellsNow:+3581 8 UIKit 0x0000000100836ae1-[UICollectionView布局子视图]+243 9 UIKit 0x0000000100311993-[UIViewCalayer代理布局层的子层:][354 10夸脱核心0x0000000104475802-[CALayer布局子层]+151 11夸脱核心0x000000010446a369(如果需要)Zn2CA516层布局交易+363 12 QuartzCore 0x000000010446a1ea\u ZN2CA58层布局和显示(如果需要)PNS\u 11事务+24 13 QuartzCore 0x00000001043ddfb8_ZN2CA7Context18提交_事务PNS_11事务+252 14夸脱矿0x00000001043df030_ZN2CA11交易承诺+394 15夸脱核心0x00000001043df69d_ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv+89 16 CoreFoundation 0x0000000101970dc7\uuuu CFRUNLOOP\u正在调用\u OUT\u以调用\u OBSERVER\u CALLBACK\u函数+23 17 CoreFoundation 0x0000000101970d37\uu cfrunloopdoobserver+391 18 CoreFoundation 0x0000000101950522\uuu CFRunLoopRun+946 19 CoreFoundation 0x000000010194fd83 CFRunLoopRunSpecific+467 20图形服务0x0000000104002f04 GSEventRunModal+161 21 UIKit 0x00000001002b1e33 UIApplicationMain+1010 22奥林波斯0x000000010000f663干管+115 23 libdyld.dylib 0x00000001025235fd开始+1 24 ??? 0x0000000000000001 0x0+1 libc++abi.dylib:以NSException类型的未捕获异常终止 lldb


您可以在init中创建一个集合视图,正确注册您的类,并将其设置为默认值

然后,该视图将被加载,加载时间晚于init,这将创建另一个集合视图并将其分配给outlet。此集合视图尚未注册类,因此失败

如果您的xib中有一个集合视图,则不清楚您为什么要费心创建集合视图,除非这是一个绝望的解决方法


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

您正在尝试注册默认的UICollectionTableViewCell。我认为您必须在尝试使用它之前对其进行子类化看起来您在xib文件中创建了表视图和集合视图,那么为什么要在initWithNibName中创建新视图?@rdelmar谢谢!这就是我的问题所在。我没有把它们作为参考 我和我正试图分配新的门店。我修复了这个问题,并将我的registerClass方法放在viewDidLoad方法中,瞧!