简体   繁体   中英

Xcode unable to dequeue a cell with identifier

my work is about `UITableView. Each time I run my project, this error appears :

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 ) :

- (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.

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.

IB

Edit 2 : Text of 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.

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];
}

Ok, your problem is that you're using Static cells , instead of Prototype cells . Just change your UITableView Content type.

原型单元选项

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 :

class : customCell

Style : Basic (but it works also with Custom)

Identifier : Cell1

Thanks for your help ssantos, Abdullah Shafique and 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.

Also, make sure the Class in the table cell's Identity inspector is set to your custom table cell subclass.

I suppose you'r using static cells. If you'r doing that consciously - just delete thous methods from your .m file:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}

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

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.

try to use customCell class instead UITableViewCell class.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell1";
customCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...

return cell;
}

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

tableView.registerClass(UITableViewCell.classForKeyedArchiver(), forCellReuseIdentifier: "your_reuse_identifier")

在我的情况下,我已经定义了缺少的UITableViewCell的故事板是本地化的。

注释掉应用程序中的所有代码:didFinishLaunchingWithOptions(AppDelegate)但保持返回YES,然后再次尝试。

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.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "YourTableViewController")
self.present(controller, animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM