简体   繁体   中英

iOS 7 TableView dequeue not working

I'm having a really annoying problem that I'm not sure WHY it's happening.

I have the Table view set, I have the correct Identifier in the Cell, but it still gives me "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" when I run it.

the code looks like this

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

    NSDictionary *event = [self.events objectAtIndex:indexPath.row];
    cell.textLabel.text = [event objectForKey:@"name"];

    return cell;
}

Any help would be great, cause I want to rip my hair. The table is connected to that class, and then Cell has that @"Cell" Identifier

Re-check all 4 steps



STEP 1: Your cellForRowAtIndexPath should look like this

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

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

NSDictionary *event = [self.events objectAtIndex:indexPath.row];
cell.textLabel.text = [event objectForKey:@"name"];

return cell;
}



STEP 2: Your "ViewController.m" File should look like this

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@end



STEP 3: PLEASE CHECK - Click on view controller move to Connection inspector - See if any other unwanted connections are present in this inspector.

您的控制器检查员应该这样



STEP 4: Tableview cell attribute inspector should like this]

您的Tableview单元格属性检查器应如下所示

Instead of:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Try:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if that does not work after

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

add:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}

Right, you need to register a cell for reuse. This can be done programmatically after you've created your table view like this:

static NSString *cellIdentifier = @"Cell";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];

Or this can also be done in Interface Builder by selecting the cell and navigating to the attributes inspector and changing the cell's id.

You are not checking for cell is allocate or not.. You can try below code..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    // add a placeholder cell while waiting on table data

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    } 
}

In the Document Outline, verify that your prototype UITableViewCell is a child of the UITableView . 情节提要文档大纲

In the Attribute Inspector, verify that your Cell Identifier and Cell Style are properly set in storyboard:

表格视图单元格属性检查器的情节提要截图

In the Identity Inspector, verify that you have the class set to UITableViewCell : 表视图单元格身份检查器的情节提要截图

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = nil;

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

        NSDictionary *event = [self.events objectAtIndex:indexPath.row];
        cell.textLabel.text = [event objectForKey:@"name"];

    }

    return cell;
}

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