ios - 从 dequeueReusableCellWithIdentifier 获取 nil

标签 ios swift uitableview

我正在使用 Storyboard来显示我的 TableViewController,而且我对此使用 Storyboard非常陌生,我通常以编程方式完成这一切。所以我遇到了一些问题。我使用以下代码导航到 Storyboard:

let registerTableViewController = RegisterTableViewController()
navigationController?.pushViewController(registerTableViewController, animated: true);

简单的两行代码,这可能是我的问题,我不能 100% 确定这就是您以编程方式移动到 Storyboard场景的方式,但当我所有的 View 都以编程方式布局时,我总是这样做,所以让我知道这是否是问题的根源。

现在,我的 tableView 有一个单元格,带有重用标识符 profileInfoCell。我已将其放入常量文件中,因此我知道我得到了正确的结果。这是我非常简单的 cellForRowAtIndexPath: 方法:

override func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier(Constants.Registration.profileInfoCellIdentifier)

    return cell!
}

问题是 cell 返回 nil。由于我使用的是 Storyboard,我认为我的设置有问题,这就是为什么我认为我可能会以错误的方式进入这个场景。如果您需要任何其他信息,请告诉我!

编辑:我没有使用静态 tableView 单元格。它们是原型(prototype)细胞。

最佳答案

是的,您实例化 View Controller 是错误的。通过像您一样创建它,它完全是程序化的,因此完全绕过 Storyboard

确保您需要在 Storyboard 中为 View Controller 场景提供标识符。

enter image description here

假设:

  • Storyboard文件名:“Main.storyboard”
  • View Controller 场景标识符:“RegisterTableViewController”

Objective-C

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
RegisterTableViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"RegisterTableViewController"];
[self presentViewController:viewController animated:YES completion:nil];

swift

let storyboard = UIStoryboard.init(name: "Main",bundle:nil)
let viewController = storyboard.instantiateViewControllerWithIdentifier("RegisterTableViewController")
self.presentViewController(viewController, animated: true, completion: nil)

关于ios - 从 dequeueReusableCellWithIdentifier 获取 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34280095/

相关文章:

swift - UITableview 中的收藏夹按钮 (Swift 4)

ios - UIImage 方向 Swift

ios - 排序数组给我错误

ios - 将 TabBar 添加到现有应用程序的最简单方法

ios - 每次显示 UIViewController 时调用一个方法

ios - 全屏 iOS 分享扩展

uitableview - 将按日期分隔的部分添加到 Swift CoreData 中的 UITableView

uitableview - (iOS)为什么导航 Controller 中的第二个 'push' segue总是崩溃?

ios - 使单元格不可点击,但其上的按钮仍应可点击

arrays - 使用 Alamofire 追加之前快速检查对象是否存在