继续从 dequeueReusableCellWithIdentifier 获得零? [英] Keep getting nil from dequeueReusableCellWithIdentifier?

查看:23
本文介绍了继续从 dequeueReusableCellWithIdentifier 获得零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在故事板文件中创建了一个标识符为mainViewTableCell"的原型单元,并将主表视图与一个名为NTTableViewController"的自定义控制器类连接起来.我在 NTTableViewController.m 中实现了函数tableView cellForRowAtIndexPath",如下所示:

I've created a prototype cell with identifier "mainViewTableCell" in storyboard file and connected the main table view with a custom controller class named "NTTableViewController". I've implemented function "tableView cellForRowAtIndexPath" in NTTableViewController.m as follows:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* MAINVIEW_CELLIDENTIFIER = @"mainViewTableCell";
    UITableViewCell *newCell = [tableView dequeueReusableCellWithIdentifier: MAINVIEW_CELLIDENTIFIER];

    if (newCell == nil) {
        newCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: MAINVIEW_CELLIDENTIFIER];
        [newCell autorelease];
        newCell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NTContactItem* currentItem = [self.contactItemContainer objectInContainerAtIndex: indexPath.row];
    NSString* firstName = currentItem.firstName;
    NSString* lastName = currentItem.lastName;

    NSString* fullName = [firstName stringByAppendingFormat: lastName];    
    [newCell.textLabel setText: fullName];
    [newCell.detailTextLabel setText: currentItem.mobilePhone];

    return newCell;
}

但我一直从 dequeueReusableCellWithIdentifier 得到 nil 并且每次都必须创建一个新的单元格实例.

But i keeping getting nil from dequeueReusableCellWithIdentifier and have to create a new instance of cell every time.

那么,怎么了?

代码:项目

提前谢谢大家.

推荐答案

对于具有原型单元格的故事板和表格视图,[tableView dequeueReusableCellWithIdentifier:] 不应返回 nil.即使这是第一个单元格,并且重用队列中已经没有单元格,tableview 也会创建原型单元格的新实例并将其返回.

With storyboards and tableviews that have prototype cells, [tableView dequeueReusableCellWithIdentifier:] should not return nil. Even if this is the very first cell, and there are no cells already in the reuse queue, the tableview will create a new instance of your prototype cell and return that.

在您的情况下,问题完全不同(我下载了您的项目,因为我真的很好奇).

In your case, the problem was something totally different (I downloaded your project because I was really curious).

在您的 application:didFinishLaunchingWithOptions: 方法中的应用程序委托中,您正在重新初始化此 tableviewcontroller.当你调用 [masterController init] 时,它会调用 [super init],而后者又会调用 [UITableViewController initWithStyle:].

In your application's delegate in your application:didFinishLaunchingWithOptions: method, you are re-initializing this tableviewcontroller. When you call [masterController init], this calls [super init], which in turn calls [UITableViewController initWithStyle:].

这会导致控制器创建一个新的 UITableView,这与故事板中的不同.新的 UITableView 没有原型单元格,这就是 dequeueReusableCellWithIdentifier: 返回 nil 的原因.

That causes the controller to create a new UITableView, which is different from the one in your storyboard. That new UITableView has no prototype cells, and so that's why dequeueReusableCellWithIdentifier: is returning nil.

课程当然是不要重新初始化已经初始化的 Objective-C 对象.当您的表格视图控制器从情节提要加载时,加载机制将使用 initWithCoder: 对其进行初始化.因此,如果您需要进行一些自定义初始化工作(例如在您的情况下设置 NSMutableArray),那么只需覆盖 initWithCoder: 和/或 awakeFromNib.

The lesson of course is to not re-initialize an Objective-C object that has already been initialized. When your table view controller is loaded from the storyboard, the loading mechanism will initialize it with initWithCoder:. So if you need to do some custom initialization work (like setting up that NSMutableArray in your case), then just override initWithCoder: and/or awakeFromNib.

您可以根据需要覆盖这些方法,但不要自己调用它们.initWithCoder:awakeFromNib 都将被 Storyboard/nib 加载机制调用.

You can override these methods as needed, but do not call them yourself. Both initWithCoder: and awakeFromNib will be called by the Storyboard/nib loading mechanism.

如果一切正确,则无需在此处以编程方式创建单元格.应该不需要这段代码:

If everything is correct, you do not need to create cells programmatically here. This bit of code should not be needed:

// This bit is unnecessary with storyboards:      
if (newCell == nil) {
    newCell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: MAINVIEW_CELLIDENTIFIER];
    [newCell autorelease];
    newCell.selectionStyle = UITableViewCellSelectionStyleNone;
}

希望对您有所帮助.

这篇关于继续从 dequeueReusableCellWithIdentifier 获得零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆