ios 无法使用标识符 Cell 使单元出列 - 必须为标识符注册一个笔尖或类,或者连接故事板中的原型单元?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23526756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
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?
提问by user3127576
I'm trying to display a TableView of a list of songs in a user's library. I used the code from this tutorial (which uses a storyboard, but I would like to try it a different way and with just a subclass of UITableView).
我正在尝试显示用户库中歌曲列表的 TableView。我使用了本教程中的代码(它使用故事板,但我想以不同的方式尝试它,并且只使用 UITableView 的一个子类)。
I get the error:
我收到错误:
*** 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'
and an error Thread 1: SIGABRT
on the line:
和Thread 1: SIGABRT
在线错误:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
This is my code:
这是我的代码:
#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;
}
The app loads and works fine, showing a blank table when I run it in the iPhone simulator on my Mac. it comes up with this error when I run it on my iPhone.
该应用程序加载并运行良好,当我在 Mac 上的 iPhone 模拟器中运行它时显示一个空白表格。当我在我的 iPhone 上运行它时,它出现了这个错误。
Any help would be much appreciated, thanks!
任何帮助将不胜感激,谢谢!
回答by rdelmar
If you create the table view programmatically, and you're just using the default UITableViewCell, then you should register the class (in viewDidLoad is a good place). You can also do this for a custom class, but only if you create the cell (and its subviews) in code (use registerNib:forCellWithReuseIdentifier: if the cell is made in a xib file).
如果您以编程方式创建表视图,并且您只是使用默认的 UITableViewCell,那么您应该注册该类(在 viewDidLoad 中是一个好地方)。您也可以为自定义类执行此操作,但前提是您在代码中创建单元格(及其子视图)(使用 registerNib:forCellWithReuseIdentifier: 如果单元格是在 xib 文件中创建的)。
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
However, this will only give you a "Basic" table view cell with no detailTextLabel. To get that type of cell, you should use the shorter dequeue method, dequeueReusableCellWithIdentifier:, which doesn't throw an exception if it doesn't find a cell with that identifier, and then have an if (cell == nil) clause to create the 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;
}
回答by Barbara R
Please, if you're using a custom class for your cellcheck that you've already registered the nib for the cell to use in your table view, as follow:
请,如果您为单元格使用自定义类,请检查您是否已经注册了要在表格视图中使用的单元格的笔尖,如下所示:
[self.yourTableViewName registerNib:[UINib nibWithNibName:@"YourNibName" bundle:nil]
forCellWithReuseIdentifier:@"YourIdentifierForCell"];
Apart from that please check that your custom UITableViewCell subclass has the appropiate reuse identifier.
除此之外,请检查您的自定义 UITableViewCell 子类是否具有适当的重用标识符。
If you're not using a custom class, please follow this instructions from Apple Docs:
如果您没有使用自定义类,请按照 Apple Docs 中的说明进行操作:
The data source, in its implementation of the tableView:cellForRowAtIndexPath: method, returns a configured cell object that the table view can use to draw a row. For performance reasons, the data source tries to reuse cells as much as possible. It first asks the table view for a specific reusable cell object by sending it a dequeueReusableCellWithIdentifier:
数据源在 tableView:cellForRowAtIndexPath: 方法的实现中,返回一个配置的单元格对象,表格视图可以使用它来绘制一行。出于性能原因,数据源尝试尽可能多地重用单元格。它首先通过向 table view 发送一个 dequeueReusableCellWithIdentifier 来询问特定的可重用单元格对象:
For complete information, please go to this link: Creating a Table View Programmatically.
有关完整信息,请转到此链接:以编程方式创建表视图。
I hope this helps!
我希望这有帮助!
回答by Fernando
The reason for the crash is that you are using
崩溃的原因是你正在使用
[tableView dequeueReusableCellWithIdentifier: forIndexPath:]
which will cause a crash unless you specify a cell or nib file to be used for the cell.
除非您指定要用于该单元格的单元格或 nib 文件,否则这将导致崩溃。
To avoid the crash, use the following line of code instead
为避免崩溃,请改用以下代码行
[tableView dequeueReusableCellWithIdentifier:];
This line of code will return nil if there is not a cell that can be used.
如果没有可以使用的单元格,这行代码将返回 nil。
You can see the difference within the docs which can be found here.
您可以在此处找到的文档中查看差异。
There is also a good answer to a Q&A that can be found here
还有一个很好的问答可以在这里找到
回答by Jsonras
I know this is a bit late to answer this question but here is what I was missing.
我知道现在回答这个问题有点晚了,但这是我所缺少的。
I was doing:
我在做:
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
But I should be doing this:
但我应该这样做:
let cell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
Notice: the self.
added next to tableView.
I hope this saves you 24 hours.
注意:在self.
tableView 旁边添加。我希望这可以为您节省 24 小时。
回答by Rachel
if you use default UITableViewCell (not customize one) [tableView dequeueReusableCellWithIdentifier: forIndexPath:], you must registerClass or registerNib; (dequeReusableCellIdentifier:forIndexPath won't return nil cell, if the cell is not there, then it will automately create a new tableViewCell with the default UITableViewCellStyelDefault!)
如果你使用默认的 UITableViewCell (不是自定义的) [tableView dequeueReusableCellWithIdentifier: forIndexPath:],你必须 registerClass 或 registerNib; (dequeReusableCellIdentifier:forIndexPath 不会返回 nil 单元格,如果单元格不存在,那么它会自动创建一个新的 tableViewCell 并使用默认的 UITableViewCellStyelDefault!)
otherwise, you can use [tableView dequeueReusableCellWithIdentifier]without registerClass or registerNib
否则,您可以使用[tableView dequeueReusableCellWithIdentifier]而不使用 registerClass 或 registerNib
回答by Kazmi
If you are using StoryBoard then select that tableviewcontroller->table->Cell, give that Cell a 'Identifier' in attribute inspector. Make sure you use the same 'Identifier' in 'cellforrowatindexpath' method.
如果您使用的是 StoryBoard,则选择该 tableviewcontroller->table->Cell,在属性检查器中为该 Cell 指定一个“标识符”。确保在“cellforrowatindexpath”方法中使用相同的“标识符”。