无法使用标识符Cell对单元格进行出列 - 必须为标识符注册nib或类,或者在故事板中连接原型单元格?

时间:2014-05-07 19:35:30

标签: ios xcode storyboard tableview

我试图在用户的库中显示歌曲列表的TableView。我使用了来自this tutoria l的代码(它使用了故事板,但我想以不同的方式尝试它,只使用UITableView的子类)。

我收到错误:

*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:5261
2014-05-07 20:28:55.722 Music App[9629:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

并在该行上显示错误Thread 1: SIGABRT

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

这是我的代码:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
    NSArray *songs = [songsQuery items];

    return [songs count];
}


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

    // Configure the cell...

    MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
    NSArray *songs = [songsQuery items];

    MPMediaItem *rowItem = [songs objectAtIndex:indexPath.row];

    cell.textLabel.text = [rowItem valueForProperty:MPMediaItemPropertyTitle];
    cell.detailTextLabel.text = [rowItem valueForProperty:MPMediaItemPropertyArtist];

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Table-view-background.png"]];
    cell.textLabel.textColor = [UIColor colorWithRed:0.278 green:0.278 blue:0.278 alpha:1.0];

    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Table-view-selected-background.png"]];

    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

该应用程序加载并正常工作,当我在Mac上的iPhone模拟器中运行它时显示一个空白表。当我在iPhone上运行它时会出现这个错误。

非常感谢任何帮助,谢谢!

6 个答案:

答案 0 :(得分:31)

如果以编程方式创建表视图,并且您只是使用默认的UITableViewCell,那么您应该注册该类(在viewDidLoad中是个好地方)。您也可以为自定义类执行此操作,但前提是您在代码中创建单元格(及其子视图)(使用registerNib:forCellWithReuseIdentifier:如果单元格是在xib文件中创建的)。

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

但是,这只会为您提供一个没有detailTextLabel的“基本”表格视图单元格。要获得该类型的单元格,您应该使用较短的出列方法dequeueReusableCellWithIdentifier :,如果找不到具有该标识符的单元格,则不会抛出异常,然后使用if(cell == nil)子句创建单元格,

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    //Configure cell
   return cell;
}

答案 1 :(得分:19)

如果您正在使用自定义类的自定义类,请检查您是否已在表格视图中注册了要使用的单元格的笔尖,如下所示:<登记/>

[self.yourTableViewName registerNib:[UINib nibWithNibName:@"YourNibName" bundle:nil]
         forCellWithReuseIdentifier:@"YourIdentifierForCell"];

除此之外,请检查您的自定义UITableViewCell子类是否具有适当的重用标识符。

如果您未使用自定义类,请按照Apple文档中的说明进行操作:

数据源在tableView:cellForRowAtIndexPath:方法的实现中,返回表视图可用于绘制行的已配置单元格对象。出于性能原因,数据源尝试尽可能多地重用单元。它首先通过向表格视图发送dequeueReusableCellWithIdentifier来询问表格视图:

有关完整信息,请访问以下链接:Creating a Table View Programmatically

我希望这有帮助!

答案 2 :(得分:11)

崩溃的原因是你正在使用

[tableView dequeueReusableCellWithIdentifier: forIndexPath:]

除非您指定要用于单元格的单元格或nib文件,否则会导致崩溃。

为避免崩溃,请使用以下代码行

[tableView dequeueReusableCellWithIdentifier:];

如果没有可以使用的单元格,这行代码将返回nil。

您可以在here找到文档中的差异。

对于可以找到here

的Q&amp; A,也有一个很好的答案

答案 3 :(得分:3)

我知道回答这个问题有点迟,但这就是我所缺少的。

我在做:

let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

但我应该这样做:

let cell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)

注意:tableView旁边添加了self.。 我希望这能为你节省24小时。

答案 4 :(得分:2)

如果使用默认的UITableViewCell(不自定义) [tableView dequeueReusableCellWithIithntifier:forIndexPath:] ,则必须使用registerClass或registerNib; (dequeReusableCellIdentifier:forIndexPath不会返回nil单元格,如果单元格不存在,那么它将使用默认的UITableViewCellStyelDefault自动创建一个新的tableViewCell!)

否则,你可以使用 [tableView dequeueReusableCellWithIdentifier] 而不使用registerClass或registerNib

答案 5 :(得分:0)

如果你正在使用StoryBoard,那么选择tableviewcontroller-&gt; table-&gt; Cell,给那个Cell一个&#39; Identifier&#39;在属性检查器中。 确保使用相同的标识符&#39; in&#39; cellforrowatindexpath&#39;方法