0

我试图在我的 iOS 应用程序中创建一个动态表,但不断收到臭名昭著的消息:

无法使标识符为 CEMBurialCell 的单元出队 - 必须为标识符注册一个 nib 或类”。

下面是我的代码不起作用。

下面是主控制器中的一些数据。

#import "CEMBurialCell.h"    // The custom cell I want to use in the tableView

@interface CEMMainViewController () <UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>
{
    UITableView *deathstoday;
}

这是viewWillAppear:方法中的一些代码。

deathstoday = [[UITableView alloc] initWithFrame:CGRectMake(1, 370, 300, 50)];
deathstoday.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
deathstoday.delegate = self;
deathstoday.dataSource = self;
deathstoday.scrollEnabled = YES;    
[self.view addSubview:deathstoday];    
[self fetchfeed];   // This populates the array for the tableview

这是viewDidLoad方法中的代码。

UINib *nib = [UINib nibWithNibName:@"CEMBurialCell" bundle:nil];    
[deathstoday registerNib:nib forCellReuseIdentifier:@"CEMBurialCell"];

这是我得到错误的地方:

无法将标识符为 CEMBurialCell 的单元格出列 - 必须为标识符注册一个 nib 或一个类,或者连接故事板中的原型单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CEMBurialCell *cell = [tableView
    dequeueReusableCellWithIdentifier:@"CEMBurialCell"  forIndexPath:indexPath];

CEMBurialCell我在方法中初始化了,viewDidLoad为什么会出现这个错误?我让这个例程在一个具有以下初始化的程序中工作:

self = [super initWithStyle:UITableViewStylePlain]; // in program that does work

而不起作用的程序有一个 init

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];   // in program that doesn't work

所以上面两行是两个程序的主要区别。我使用了自定义单元格而不是UITableView单元格,因为我的单元格中需要一个 actionBlock。那么为什么第二个程序在我的自定义单元格上出现错误?相同的自定义单元格CEMBurialCell在另一个程序中工作。我错过了什么?

4

1 回答 1

0

Your problem is simple. In viewDidLoad, deathstoday is nil so you don't actually register the nib.

Why do you wait until viewWillAppear: to create the table view? You should create the table view in viewDidLoad before your call to registerNib:forCellReuseIdentifier:.

Just be sure to base the table view's frame on self.view.bounds so the autoresizingMask works as expected.

于 2015-12-14T23:29:23.353 回答