ios 为什么当我的 UITableView 尝试加载时会出现无法出队的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15773275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting an error about being unable to dequeue when my UITableView tries to load?
提问by Doug Smith
I get the following error:
我收到以下错误:
*Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier FontCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将具有标识符 FontCell 的单元格出列 - 必须为标识符注册一个笔尖或类,或连接故事板中的原型单元格”
I'm not sure exactly what I'm doing wrong. I set the cell identifier (programmatically, as it's not created through Interface Builder) and do everything I thought I was supposed to do in the delegate methods, but I'm still getting that error when I try to get the UITableView to load.
我不确定我做错了什么。我设置了单元格标识符(以编程方式,因为它不是通过 Interface Builder 创建的)并执行我认为应该在委托方法中执行的所有操作,但是当我尝试加载 UITableView 时仍然遇到该错误。
Here's the relevant code (it's worth noting I've subclassed UITableViewCell for customization options):
这是相关的代码(值得注意的是,我已经将 UITableViewCell 子类化为自定义选项):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.fonts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"FontCell";
FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[FontCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FontCell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
int row = indexPath.row;
cell.fontFamilyLabel.text = self.fonts[row];
return cell;
}
And here's the only method I changed in my subclassed UITableViewCell (FontCell):
这是我在子类 UITableViewCell (FontCell) 中更改的唯一方法:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.fontFamilyLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 200, 20)];
self.fontFamilyLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.fontFamilyLabel];
}
return self;
}
What exactly am I doing wrong?
我到底做错了什么?
回答by cscott530
Easiest fix is to just change it to FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
As with your current code, you'll have to check to be sure cell
is not nil
if you do this method.
最简单的解决方法是将其更改为FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
As with your current code,如果您使用此方法,则必须检查以确保cell
不是nil
。
Alternately, you can register a UINib
or Class
at the table level that is tied to @"FontCell"
或者,您可以在绑定到的表级别注册一个UINib
或Class
@"FontCell"
For example (up in viewDidLoad
):
例如(在 中viewDidLoad
):
[self.tableView registerClass: [FontCell class] forCellReuseIdentifier:@"FontCell"];
Then you can do
然后你可以做
FontCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FontCell" forIndexPath:indexPath];
The nice thing with this method is that you know that your cell will never be nil
, so you can just immediately begin modifying it.
这种方法的好处是你知道你的单元格永远不会是nil
,所以你可以立即开始修改它。
回答by Lithu T.V
You're using the dequeueReusableCellWithIdentifier:forIndexPath:
method. The documentation for that method says this:
您正在使用该dequeueReusableCellWithIdentifier:forIndexPath:
方法。该方法的文档是这样说的:
You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
So here
所以在这里
[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"];
回答by offset
I also had such a problem, and the solution I found is:
我也遇到了这样的问题,我找到的解决方法是:
Go to the Project Navigator and select “ViewController.h”. Append
转到项目导航器并选择“ViewController.h”。附加
<UITableViewDelegate, UITableViewDataSource>
after “UIViewController”.
在“UIViewController”之后。
回答by masteroleary
If using a tableview (not a table view controller) as I was, there are not cells that appear by default.
如果像我一样使用 tableview(不是 table view controller),默认情况下不会出现单元格。
In the storyboard select the tableview Open the Attributes inspector Change prototype cells from 0 to 1 Select the newly displayed table cell In the Attributes inspector set the Identitifier to "FontCell"
在故事板中选择 tableview 打开属性检查器将原型单元格从 0 更改为 1 选择新显示的表格单元格在属性检查器中将标识符设置为“FontCell”