ios Xcode 无法将带有标识符的单元格出列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19084274/
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
Xcode unable to dequeue a cell with identifier
提问by GoldXApp
my work is about `UITableView. Each time I run my project, this error appears :
我的工作是关于`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
I checked a hundred times my cell identifier in my storyboard and in my code are the same.
Code (defaut code from UITableViewController
) :
我在我的故事板和我的代码中检查了一百次我的单元格标识符是相同的。代码(来自 的默认代码UITableViewController
):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
Picture of Table View Cell properties :
表格视图单元格属性的图片:
I created and implemented a subclass of UITableViewCell
for my cell.
我UITableViewCell
为我的单元创建并实现了一个子类。
Any idea why this is not working ?
知道为什么这不起作用吗?
Any way (line of code) to know what is the identifier of a cell ?
任何方式(代码行)知道单元格的标识符是什么?
Thanks
谢谢
Edit : Screenshot of my interface builder.
编辑:我的界面构建器的屏幕截图。
Edit 2 : Text of customCell.h
编辑 2:customCell.h 的文本
#import <UIKit/UIKit.h>
@interface customCell : UITableViewCell
@end
New error appears when I run the project :
运行项目时出现新错误:
[<choixActiviteViewController 0x7591ac0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Cell1.
choixActiviteViewController is a subclass of UITableViewController and is the custom class of Choix Activite View Controller.
choixActiviteViewController 是 UITableViewController 的子类,是 Choix Activite View Controller 的自定义类。
回答by Abdullah Shafique
Instead of:
代替:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
Try:
尝试:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if this does not work then, also add:
如果这不起作用,那么还要添加:
if (cell == nil) {
cell = [[customCell alloc] init];
}
回答by ssantos
Ok, your problem is that you're using Static cells
, instead of Prototype cells
. Just change your UITableView Content type.
好的,您的问题是您使用的是Static cells
, 而不是Prototype cells
. 只需更改您的 UITableView 内容类型。
回答by GoldXApp
Ok, my project finally works. Some errors appeared about things I've deleted and I can't find anymore.
好的,我的项目终于成功了。关于我已删除的内容出现了一些错误,但我再也找不到了。
I've just deleted every TableViewCell I had to create a new unique UITableViewCell
with the following properties :
我刚刚删除了每个 TableViewCell 我必须创建一个UITableViewCell
具有以下属性的新的唯一项:
class : customCell
类:customCell
Style : Basic (but it works also with Custom)
风格:基本(但它也适用于自定义)
Identifier : Cell1
标识符 : Cell1
Thanks for your help ssantos, Abdullah Shafique and bilobatum.
感谢您对 ssantos、Abdullah Shafique 和 bilobatum 的帮助。
回答by bilobatum
If your table cell is a subclass of UITableViewCell, then you're not using the "Basic" style of cell. Change the style setting in the attributes inspector to Custom.
如果您的表格单元格是 UITableViewCell 的子类,那么您没有使用单元格的“基本”样式。将属性检查器中的样式设置更改为自定义。
Also, make sure the Class in the table cell's Identity inspector is set to your custom table cell subclass.
此外,请确保表格单元格的身份检查器中的类设置为您的自定义表格单元格子类。
回答by serg_ov
I suppose you'r using static cells. If you'r doing that consciously - just delete thous methods from your .m file:
我想您正在使用静态单元格。如果您有意识地这样做 - 只需从您的 .m 文件中删除 thous 方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
回答by Prof Von Lemongargle
If you are intentionally using Static UITableViewCells, then you don't have to implement the normal UITableView population methods (numberOfSectionsInTableView:, tableView:numberOfRowsInSection:, tableView:cellForRowAtIndexPath:).
The UITableView will automatically populate from the cells defined in your storyboard. You do end up using the tableView:cellForRowAtIndexPath: in order to format the data in the cells. You use [super tableView:cellForRowAtIndexPath:] to get the tableViewCell and then ReuseIdentifier, tags, or indexPath to match the cell to the data that goes with it.
如果您有意使用静态 UITableViewCells,那么您不必实现正常的 UITableView 填充方法(numberOfSectionsInTableView:, tableView:numberOfRowsInSection:, tableView:cellForRowAtIndexPath:).
UITableView 将自动从故事板中定义的单元格填充。您最终会使用 tableView:cellForRowAtIndexPath: 来格式化单元格中的数据。您使用 [super tableView:cellForRowAtIndexPath:] 获取 tableViewCell,然后使用 ReuseIdentifier、标签或 indexPath 将单元格与随附的数据进行匹配。
回答by Oleg Sobolev
try to use customCell
class instead UITableViewCell
class.
尝试使用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;
}
回答by arango_86
The reason for this issue is very clear from the exception:
这个问题的原因从异常就很清楚了:
One Solution to this problem is register a class for the identifier
这个问题的一个解决方案是为标识符注册一个类
In Swift this can be done as follows
在 Swift 中,这可以按如下方式完成
tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")
回答by Fran Pugl
In my case the Storyboard where I had defined the missing UITableViewCell was localised.
在我的情况下,我定义丢失的 UITableViewCell 的 Storyboard 已本地化。
回答by HRY
I was facing this issue when presenting the UITableViewController having a custom cell from another viewcontroller. As a workaround I instantiated it from the storyboard and that solved the issue.
在展示具有来自另一个视图控制器的自定义单元格的 UITableViewController 时,我遇到了这个问题。作为一种解决方法,我从情节提要中实例化了它并解决了问题。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourTableViewController")
self.present(controller, animated: true, completion: nil)