objective-c - 继续从 dequeueReusableCellWithIdentifier 获取 nil?

标签 objective-c ios xcode uikit tableview

我在 Storyboard文件中创建了一个带有标识符“mainViewTableCell”的原型(prototype)单元,并将主 TableView 与一个名为“NTTableViewController”的自定义 Controller 类连接起来。 我在 NTTableViewController.m 中实现了函数“tableView cellForRowAtIndexPath”,如下所示:


- (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 并且每次都必须创建一个新的单元格实例。

那么,有什么问题呢?

代码:project

提前谢谢大家。

最佳答案

对于具有原型(prototype)单元格的 Storyboard和表格 View ,[tableView dequeueReusableCellWithIdentifier:] 不应返回 nil。即使这是第一个单元格,并且重用队列中已经没有单元格,tableview 也会创建原型(prototype)单元格的新实例并返回它。

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

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

这会导致 Controller 创建一个新的 UITableView,它与 Storyboard中的不同。新的 UITableView 没有原型(prototype)单元格,所以这就是 dequeueReusableCellWithIdentifier: 返回 nil 的原因。

类(class)当然是不要重新初始化已经初始化的 Objective-C 对象。当您的 TableView Controller 从 Storyboard加载时,加载机制将使用 initWithCoder: 对其进行初始化。因此,如果您需要执行一些自定义初始化工作(例如在您的情况下设置 NSMutableArray),则只需覆盖 initWithCoder: 和/或 awakeFromNib

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

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

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

希望对您有所帮助。

关于objective-c - 继续从 dequeueReusableCellWithIdentifier 获取 nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9797821/

相关文章:

ios - iOS-7 中 swift 代码静态库中 ObjC 函数的 Swift 接口(interface)

ios - Appium 错误 Xcode 找不到匹配 'com.facebook.WebDriverAgentRunner' 的配置文件

iphone - nivo slider 图像未出现在 ipod/iphone/ipad 上

ios - 通过 Cocoapods 设置 CVCalendar : Views Aren't Showing

iphone - 修复 uiview 在 iphone sdk 中的位置

ios - 如何删除 UITableView 上方的多余空格?

objective-c - 在 SpriteKit 中使用 SKLabelNode 插入换行符

ios - iOS 模拟器可以记住使用 Retina iPhone 和非 Retina iPad 吗?

c - 将 C 库与 Swift 结合使用(多指针及所有)

iphone - 如何通过传递手机号码作为参数从地址簿中获取人员的联系人图像