为什么我的UITableView尝试加载时出现无法出队的错误? [英] Why am I getting an error about being unable to dequeue when my UITableView tries to load?

查看:104
本文介绍了为什么我的UITableView尝试加载时出现无法出队的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用,原因:'无法使用标识符FontCell将单元格
出列 - 必须为
标识符注册一个nib或类,或者在故事板中连接原型单元'


我不确定我到底做错了什么。我设置了单元格标识符(以编程方式,因为它不是通过Interface Builder创建的)并执行我认为我应该在委托方法中执行的所有操作,但是当我尝试加载UITableView时,我仍然会收到该错误。 / p>

以下是相关代码(值得注意的是我已将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 {
静态的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];

返回单元格;
}

这是我在子类UITableViewCell(FontCell)中更改的唯一方法:

   - (ID)initWithStyle:(UITableViewCellStyle)样式reuseIdentifier:(的NSString *)reuseIdentifier 
{
自= [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];
}
返回自我;
}

我究竟做错了什么?


< DIV类= h2_lin>解决方案

最简单的解决方法是只将其更改为 FontCell *细胞= [的tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 如同您当前的代码,如果您执行此方法,则必须检查以确保 cell 不是 nil 。 / p>




或者,您可以注册 UINib 在表级别与 @FontCell相关联



例如(在 viewDidLoad 中):

  [self.tableView registerClass :[FontCell类] forCellReuseIdentifier:@FontCell]; 

然后你可以做到

  FontCell * cell = [tableView dequeueReusableCellWithIdentifier:@FontCellforIndexPath:indexPath]; 

这种方法的好处是你知道你的手机永远不会 nil ,所以你可以马上开始修改它。


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'

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.

Here's the relevant code (it's worth noting I've subclassed UITableViewCell for customization options):

- (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):

- (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?

解决方案

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.


Alternately, you can register a UINib or Class at the table level that is tied to @"FontCell"

For example (up in 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.

这篇关于为什么我的UITableView尝试加载时出现无法出队的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆