【问题标题】:Xcode unable to dequeue a cell with identifierXcode 无法将具有标识符的单元格出列
【发布时间】:2013-10-05 17:30:05
【问题描述】:

我的工作是关于 `UITableView.每次我运行我的项目时,都会出现这个错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell1 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

我检查了一百次我的故事板中的单元格标识符和我的代码中的相同。 代码(来自UITableViewController的默认代码):

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

    // Configure the cell...

    return cell;
}

表格视图单元格属性的图片:

我为我的单元创建并实现了UITableViewCell 的子类。

知道为什么这不起作用吗?

任何方式(代码行)知道单元格的标识符是什么?

谢谢

编辑:我的界面构建器的屏幕截图。

编辑 2:customCell.h 的文本

#import <UIKit/UIKit.h>

@interface customCell : UITableViewCell

@end

运行项目时出现新错误:

[<choixActiviteViewController 0x7591ac0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Cell1.

choixActiviteViewController 是 UITableViewController 的子类,是 Choix Activite View Controller 的自定义类。

【问题讨论】:

  • 请附上一些相关代码。属性检查器的屏幕截图也会很有用。
  • 你是如何实例化你的视图控制器的?如果它没有通过 segue 或 [UIStoryboard instantiateViewControllerWithIdentifier:] 从情节提要中实例化,则您的情节提要原型将不会被注册。
  • 这个UITableViewController是我的第一个视图。之前只有一个导航控制器,关系为“rootviewcontroller”。
  • 您是否检查过 tableView 是否正确链接到控制器?
  • @TimothyMoose.. 你先生,今晚是英雄.. 我心不在焉地通过 alloc/init 启动了我的测试 tableViewController 并花了一个小时试图弄清楚为什么我的原型被忽略了.. 谢谢..

标签: ios xcode uitableview identifier


【解决方案1】:

代替:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

试试:

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

如果这不起作用,还添加:

  if (cell == nil) {
    cell = [[customCell alloc] init];
}

【讨论】:

  • 它不会改变任何东西。
  • 它在继承 UITableViewController 时起作用。可悲的是,苹果觉得有必要在其中编写容易出错的代码。
【解决方案2】:

好的,您的问题是您使用的是Static cells,而不是Prototype cells。只需更改您的 UITableView 内容类型。

【讨论】:

  • 每个单元格出现错误:连接“Cell1”不能将原型对象作为其目标。
  • 实际上,您应该只有一个 Prototype 单元格。到底哪个是错误?
【解决方案3】:

好的,我的项目终于成功了。我删除的内容出现了一些错误,我再也找不到了。

我刚刚删除了每个 TableViewCell 我必须创建一个新的唯一 UITableViewCell 具有以下属性:

类:customCell

样式:基本(但也适用于自定义)

标识符:Cell1

感谢您的帮助 ssantos、Abdullah Shafique 和 bilobatum。

【讨论】:

  • 很高兴您找到了解决方案!
  • 根据你的实验,我刚刚清理了我的项目,错误消失了!!
【解决方案4】:

如果您的表格单元格是 UITableViewCell 的子类,那么您没有使用“基本”样式的单元格。将属性检查器中的样式设置更改为自定义。

另外,请确保表格单元的身份检查器中的类设置为您的自定义表格单元子类。

【讨论】:

  • 我已更改为“基本样式”以编辑 TableViewCell 中的“标题”。我尝试了“自定义”类型,但又出现了同样的错误。
  • 从您的屏幕截图中,您的单元格原型看起来不像是 UITableView 单元格的子类。所以你没有继承 UITableViewCell?
  • 是的,我想是的。在我的 IB 中,每个单元格在其自定义类中都有“customCell”(在身份检查器中)。在 customCell.h 中:@interface customCell:UITableViewCell
  • 这里有一个解决问题的想法:看看是否可以在不将原型子类化 UITableViewCell 的情况下加载表格单元格。
【解决方案5】:

我想您使用的是静态单元格。如果您有意识地这样做 - 只需从 .m 文件中删除 thous 方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}

【讨论】:

    【解决方案6】:

    尝试使用customCell 类而不是UITableViewCell 类。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    static NSString *CellIdentifier = @"Cell1";
    customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
    }
    

    【讨论】:

      【解决方案7】:

      如果你有意使用静态 UITableViewCells,那么你不必实现普通的 UITableView 填充方法(numberOfSectionsInTableView:, tableView:numberOfRowsInSection:, tableView:cellForRowAtIndexPath:).
      UITableView 将自动从故事板中定义的单元格中填充。您最终会使用 tableView:cellForRowAtIndexPath: 来格式化单元格中的数据。您使用 [super tableView:cellForRowAtIndexPath:] 获取 tableViewCell,然后重用标识符、标签或 indexPath 以将单元格与随附的数据匹配。

      【讨论】:

        【解决方案8】:

        这个问题的原因从异常中就很清楚了:

        这个问题的一个解决方案是 为标识符注册一个类

        在 Swift 中,可以按如下方式完成

        tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")
        

        【讨论】:

          【解决方案9】:

          在我的例子中,我定义丢失的 UITableViewCell 的情节提要已本地化。

          【讨论】:

            【解决方案10】:

            将 application:didFinishLaunchingWithOptions(AppDelegate) 中的所有代码注释掉,但仍然返回 YES,然后再试一次。

            【讨论】:

              【解决方案11】:

              在展示具有来自另一个视图控制器的自定义单元格的 UITableViewController 时,我遇到了这个问题。作为一种解决方法,我从情节提要中对其进行了实例化,从而解决了问题。

              let storyboard = UIStoryboard(name: "Main", bundle: nil)
              let controller = storyboard.instantiateViewController(withIdentifier: "YourTableViewController")
              self.present(controller, animated: true, completion: nil)
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-03-02
                • 2015-01-23
                • 1970-01-01
                • 2016-07-09
                • 2019-02-19
                相关资源
                最近更新 更多