Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

You're using the dequeueReusableCellWithIdentifier:forIndexPath: method. The documentation for that method says this:

Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.

You didn't register a nib or a class for the reuse identifier "Cell".

Looking at your code, you seem to expect the dequeue method to return nil if it doesn't have a cell to give you. You need to use the dequeueReusableCellWithIdentifier: for that behavior:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

Notice that dequeueReusableCellWithIdentifier: and dequeueReusableCellWithIdentifier:forIndexPath: are different methods. See doc for the former and the latter.

If you want to understand why you'd want to ever use dequeueReusableCellWithIdentifier:forIndexPath:, check out this Q&A.


I think this error is about registering your nib or class for the identifier.

So that you may keep what you are doing in your tableView:cellForRowAtIndexPath function and just add code below into your viewDidLoad:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

It's worked for me. Hope it may help.


Although this question is fairly old, there is another possibility: If you are using Storyboards, you simply have to set the CellIdentifier in the Storyboard.

So if your CellIdentifier is "Cell", just set the "Identifier" property: enter image description here

Make sure to clean your build after doing so. XCode sometimes has some issues with Storyboard updates


i had the same problem replacing with

static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

solved