简体   繁体   English

自定义表格视图单元格:IBOutlet 标签为 nil

[英]Custom table view cell: IBOutlet label is nil

Consider the following simple view controller:考虑以下简单的视图控制器:

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    var items = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
        self.tableView.dataSource = self
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell") as CustomTableViewCell
        cell.titleLabel!.text = self.items[indexPath.row]
        return cell
    }
}

And custom cell view:和自定义单元格视图:

class CustomTableViewCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!   
}

This code causes the following error.此代码导致以下错误。

fatal error: unexpectedly found nil while unwrapping an Optional value致命错误:在展开可选值时意外发现 nil

titleLabel is nil — it's not clear why. titleLabel为 nil — 不清楚原因。 Setting default properties of UITableViewCell (like textLabel ) work just fine.设置UITableViewCell的默认属性(如textLabel )工作得很好。

I'm using a nib for the custom cell.我正在为自定义单元格使用笔尖。

Both the labels and table views are correctly connected to their IBOutlet s.标签和表格视图都正确连接到它们的IBOutlet

标签表视图

Both the prototype cell and the custom nib view are marked as having a CustomTableViewCell class.原型单元格和自定义 nib 视图都被标记为具有CustomTableViewCell类。

I'm new to iOS development, am I missing something obvious?我是 iOS 开发的新手,我是否遗漏了一些明显的东西?

Sample Xcode project available . 示例 Xcode 项目可用

First off, you're using a nib file to load your custom cell into the table.首先,您使用 nib 文件将自定义单元格加载到表格中。 That's probably going to be more of a headache than it's worth if you're new to Swift/Cocoa.如果您是 Swift/Cocoa 的新手,这可能会更令人头疼。 I would move everything over to storyboard for the time being.我暂时将所有内容移至情节提要。 Instead of using a nib file click, go to Storyboard, click on your UITableView and make sure the TableView's content setting is Dyanamic Prototypes :不要使用 nib 文件单击,而是转到 Storyboard,单击您的UITableView并确保 TableView 的内容设置为Dyanamic Prototypes

在此处输入图像描述

Next, click on the prototype cell (the only one in the table view) and set the class to CustomTableViewCell and set its reuse identifier to customCell :接下来,单击原型单元格(表格视图中的唯一一个)并将类设置为CustomTableViewCell并将其重用标识符设置为customCell

在此处输入图像描述在此处输入图像描述

Next, add a label to your prototype cell and link it to the IBOutlet in your CustomTableViewCell class.接下来,将标签添加到原型单元格并将其链接到CustomTableViewCell类中的 IBOutlet。 You don't need to register your customCell so long as you've set the reuse identifier in storyboard.只要您在情节提要中设置了重用标识符,您就不需要注册您的customCell Delete this line:删除这一行:

self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")

and it should run.它应该运行。

try this尝试这个

import UIKit

class ViewController: UIViewController, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    var items = ["One", "Two", "Three"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerNib(UINib(nibName: "CustomTableViewCell", bundle: nil), forCellReuseIdentifier: "customCell")// CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
        self.tableView.dataSource = self
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCellWithIdentifier("customCell", forIndexPath: indexPath) as CustomTableViewCell
        cell.titleLabel!.text = self.items[indexPath.row]
        return cell
    }
}

Register your nib like this:像这样注册你的笔尖:

let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")

You should use dequeueReusableCellWithIdentifier:forIndexPath to dequeue the cells.您应该使用dequeueReusableCellWithIdentifier:forIndexPath将单元格出列。

If you are using storyboard to create the cells, you should not need to register the class for reuse as storyboard does it for you if you set the reuseIdentifier.如果您使用情节提要创建单元格,则不需要注册该类以进行重用,因为情节提要为您执行此操作,如果您设置了重用标识符。

Please make sure you are not doing any mistake while registering your nib(custom cell) in the viewdidload.请确保您在 viewdidload 中注册您的 nib(自定义单元格)时没有犯任何错误。 Nib name and reuseIdentifiers should not be wrong. Nib name 和reuseIdentifiers 应该没有错。

The Outlet in your CustomTableViewCell must have strong reference. CustomTableViewCell中的Outlet必须具有强引用。

Replace 更换

@IBOutlet weak var titleLabel: UILabel! 

with

@IBOutlet var titleLabel: UILabel! 

Works for me without !对我有用!

EDIT编辑

    let challenge = self.challenges![indexPath.row]

    let title = challenge["name"]
    if title != nil {
        cell.titleLabel.text = title
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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