I have a default master-detail table view with a menu. The menu works, but for some reason I get the following error when pressing the '+' button to add another cell in my app:
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'
I searched for a solution for days
If I add to viewDidLoad
:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
I don't get the error, but the cells that are added aren't the ones configured in my storyboard, nor do they point to the detail view controller.
I have checked multiple times if the identifier field for the prototype cell has been filled in and is the same as *cellIdentifier.
From what I know it seems of the same nature as the question asked here.
Any help would be appreciated and if you have the time please explain why it's wrong what I did or why certain code should be added.
Requested code (both are default code added by 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;
}
Also check out the pictures and project files added in the comment
Replace the following line in [MAAppDelegate application:didFinishLaunching]
UINavigationController *masterViewController = [[UINavigationController alloc] initWithRootViewController:[MAMasterViewController new]];
with
UINavigationController *masterViewController = (UINavigationController *)self.window.rootViewController;
When the app reaches didFinishLaunching
, the root view controller has already been configured from the storyboard (if you look at your storyboard, the root view controller is the one with the arrow pointing to it). Note that this process relies on the initWithCoder:
initializer.
In didFinishLaunching
you're discarding this view controller, replacing it with an instance of TWTSideMenuViewController
, which you've configured with new, non-storyboard instances of UINavigationController
and MAMasterViewController
. If you think about it, there's no way for [MAMasterViewController new]
to know that it should be configured from a particular storyboard element. There is actually an API for instantiating view controllers from the storyboard: [storyboard instantiateViewControllerWithIdentifier:]
. But you don't need to do that since it's already been done for you.
So the solution is simple. Just configure TWTSideMenuViewController
with the existing root view controller self.window.rootViewController
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With