Ios 无法使用标识符标题取消单元格的命名

Ios 无法使用标识符标题取消单元格的命名,ios,objective-c,uitableview,Ios,Objective C,Uitableview,当我按下打开tableview的按钮时,出现以下错误: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier title - must register a nib or a class for the identifier or connect a prototype cell in a sto

当我按下打开tableview的按钮时,出现以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier title - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
以下是视图控制器中tableview的代码以及导致问题的方法:

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

NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

return cell;
}
我研究了错误,并尝试删除
forindexath:indexath
,因此代码如下所示:

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

NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

return cell;
}
现在,这导致了一个新的错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:
现在我做了一些日志记录,以发现
cell==nil
是正确的,所以我添加了一个检查,正如前面一些问题所建议的:

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

NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
     cell = [[UITableViewCell alloc] init]
}

return cell;
}
现在这删除了所有错误,但是现在当我打开tableview时,当我想要在故事板中创建的单元格时,单元格是空的

如何解决此问题

下面是情节提要中视图控制器的外观:

如果要在故事板中创建原型单元,则需要将它们的“标识符”字段设置为“单元标识符”字符串。您可以通过选择单元格并查看属性检查器来完成此操作

如果要为UITableViewCell创建单独的.xib文件,则需要在代码中的某个位置对UITableView调用此方法:

registerNib:forCellReuseIdentifier:
如果您在代码中执行所有操作,并且只使用知道如何布局自身的UITableViewCell子类,则需要在UITableView上调用此方法:

registerClass:forCellReuseIdentifier:

这里是参考的链接

您可以在UITable上调用两种单元格回收方法 观点

这有点令人困惑,但它们在使用方式上有很大不同。接受第二个参数(nsindepath类型)的参数取决于您是否首先向tableView注册了一个类或xib文件,以便tableView可以在没有方便回收的单元格时为您创建一个临时单元格。第一个方法将始终返回一个单元格,因此您可以为cellForRowAtIndexPath编写代码:就像您所做的那样

第二个方法(只接受一个参数,((NSString*)cellIdentifier)可以并且将在没有可回收的单元格时返回nil。因此,当您使用此方法时,您应该测试结果是否为nil,并在这种情况下创建一个单元格。 乙二醇


为了利用您的单元格原型,您需要为每一行/每一节注册一个类或xib,以便表格知道要创建哪个单元格。只有在创建了足够多的单元格以填充屏幕并开始滚动时,回收材料才会真正起作用。祝您好运

菜单项包含什么?您是否设置了相应的值通过Xcode的界面设计器为您要创建的每个不同单元生成单元标识符?@Romain是的,故事板中的每个单元都有一个与菜单项中的一个条目对应的标识符。这不是一个有效的问题,但从外观上看,您最好只使用静态TBVC,而不是使用原型好的。@theMonster好的,我把它换成了静态单元格,我想你是对的,它可能很适合这个目的。尽管我认为问题仍然存在,你将如何使用原型单元格。这解释了代码中发生的事情,我试过了,它成功了。谢谢!
-(UITableViewCell *) dequeueReusableCellWithIdentifier:forIndexPath:
-(UITableViewCell *)dequeueReusableCellWithIdentifier:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  static NSString *cellId = @"cellID";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];

  if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
  }

 //etc etc decorate your cell...

  return cell;
}