【问题标题】:Why am I getting an error about being unable to dequeue when my UITableView tries to load?当我的 UITableView 尝试加载时,为什么会出现无法出列的错误消息?
【发布时间】:2013-03-24 06:55:10
【问题描述】:

我收到以下错误:

* 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使单元格出列 带有标识符 FontCell - 必须为 标识符或连接故事板中的原型单元'

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

以下是相关代码(值得注意的是,我将 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;
}

这是我在子类 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;
}

我到底做错了什么?

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

最简单的解决方法是将其更改为 FontCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 与您当前的代码一样,如果您使用此方法,您必须检查以确保 cell 不是 nil


或者,您可以在绑定到@"FontCell" 的表级别注册UINibClass

例如(上viewDidLoad):

[self.tableView registerClass: [FontCell class] forCellReuseIdentifier:@"FontCell"];

那你就可以了

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

这种方法的好处是你知道你的单元格永远不会是nil,所以你可以立即开始修改它。

【讨论】:

  • 如果我更改每个单元格的标识符会怎样。我不认为它会起作用还有其他方法吗?
【解决方案2】:

您正在使用dequeueReusableCellWithIdentifier:forIndexPath: 方法。该方法的文档是这样说的:

You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

所以这里

[self.tableView registerClass: [FontCell class] forReuseIdentifier: @"FontCell"];

【讨论】:

  • 大概应该注册他的FontCell类,这是UITableViewCell的子类
  • @Brian 是的,当使用自定义单元格时注册该单元格
  • 应该是[self.tableView registerClass:[FontCell class] forCellReuseIdentifier:@"FontCell"];
【解决方案3】:

我也遇到过这样的问题,我找到的解决方法是:

转到项目导航器并选择“ViewController.h”。追加

 <UITableViewDelegate, UITableViewDataSource>

在“UIViewController”之后。

【讨论】:

  • 这是什么?那是做什么的?
【解决方案4】:

如果像我一样使用表格视图(不是表格视图控制器),则默认情况下不会出现单元格。

在情节提要中选择表格视图 打开属性检查器 将原型单元从 0 更改为 1 选择新显示的表格单元格 在属性检查器中将标识符设置为“FontCell”

【讨论】:

    猜你喜欢
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    相关资源
    最近更新 更多