gpt4 book ai didi

ios - 必须为标识符注册一个 nib 或一个类或连接一个原型(prototype)单元

转载 作者:可可西里 更新时间:2023-11-01 03:58:16 27 4
gpt4 key购买 nike

我有一个带有 menu 的默认主从 TableView .菜单有效,但由于某种原因,我在按“+”按钮在我的应用程序中添加另一个单元格时收到以下错误:

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'

我搜索了好几天的解决方案

如果我添加到viewDidLoad:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

我没有收到错误,但添加的单元格不是我的 Storyboard 中配置的单元格,也没有指向详细 View Controller 。

我多次检查了原型(prototype)单元格的标识符字段是否已填写并且与*cellIdentifier 相同。

据我所知,这似乎与问题的性质相同here .

如有任何帮助,我们将不胜感激,如果您有时间,请解释为什么我所做的是错误的,或者为什么应该添加某些代码。

请求代码(都是Xcode 5.0.1添加的默认代码):

// Code to add the button

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;

// Code called when button is pressed:

- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}

另请查看评论中添加的图片和项目文件

最佳答案

替换[MAAppDelegate application:didFinishLaunching]中的以下行

UINavigationController *masterViewController = [[UINavigationController alloc] initWithRootViewController:[MAMasterViewController new]];

UINavigationController *masterViewController = (UINavigationController *)self.window.rootViewController;

当应用程序到达 didFinishLaunching 时, Root View Controller 已经从 Storyboard 中进行了配置(如果您查看您的 Storyboard , Root View Controller 就是箭头指向它的那个)。请注意,此过程依赖于 initWithCoder: 初始化程序。

didFinishLaunching 中,您将丢弃此 View Controller ,将其替换为 TWTSideMenuViewController 的实例,您已使用 的新的 Non-Storyboard 实例对其进行配置>UINavigationControllerMAMasterViewController。如果您考虑一下,[MAMasterViewController new] 无法知道它应该从特定的 Storyboard 元素进行配置。实际上有一个用于从 Storyboard实例化 View Controller 的 API:[storyboard instantiateViewControllerWithIdentifier:]。但您不需要这样做,因为它已经为您完成了。

所以解决方法很简单。只需使用现有的 Root View Controller self.window.rootViewController 配置 TWTSideMenuViewController

关于ios - 必须为标识符注册一个 nib 或一个类或连接一个原型(prototype)单元,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com