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

我正在尝试在用户的库中显示歌曲列表的TableView。 我使用了这个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上运行它时会出现此错误。

任何帮助将不胜感激,谢谢!

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

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

但是,这只会为您提供一个没有detailTextLabel的“基本”表格视图单元格。 要获得该类型的单元格,您应该使用较短的出列方法dequeueReusableCellWithIdentifier :,如果找不到具有该标识符的单元格,则不会抛出exception,然后使用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; } 

如果您使用自定义类进行单元格检查,请检查您是否已在表格视图中注册要使用的单元格的笔尖,如下所示:

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

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

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

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

有关完整信息,请转到以下链接: 以编程方式创建表视图 。

我希望这有帮助!

崩溃的原因是你正在使用

 [tableView dequeueReusableCellWithIdentifier: forIndexPath:] 

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

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

 [tableView dequeueReusableCellWithIdentifier:]; 

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

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

在这里可以找到问答的答案

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

否则,您可以使用[tableView dequeueReusableCellWithIdentifier]而无需registerClass或registerNib

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

我在做:

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

但我应该这样做:

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

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

如果您正在使用StoryBoard,那么选择tableviewcontroller-> table-> Cell,在属性检查器中为该Cell指定一个’Identifier’。 确保在’cellforrowatindexpath’方法中使用相同的’Identifier’。

Interesting Posts