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

时间:2015-03-17 13:55:34

标签: ios uitableview swift interface-builder

考虑以下简单视图控制器:

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
    }
}

自定义单元格视图:

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

此代码导致以下错误。

  

致命错误:在解包可选值时意外发现nil

titleLabel是零 - 目前尚不清楚原因。设置UITableViewCell的默认属性(如textLabel)就可以了。

我正在使用nib作为自定义单元格。

标签和表格视图都正确连接到IBOutlet

label table view

原型单元格和自定义笔尖视图都标记为具有CustomTableViewCell类。

我是iOS开发的新手,我错过了一些明显的东西吗?

Sample Xcode project available

8 个答案:

答案 0 :(得分:48)

首先,您使用nib文件将自定义单元格加载到表格中。如果你是Swift / Cocoa的新手,这可能会比它更值得头疼。我暂时将所有内容都移到故事板上。不要使用nib文件,请转到Storyboard,单击UITableView并确保TableView的内容设置为Dyanamic Prototypes

enter image description here

接下来,单击原型单元格(表格视图中唯一的一个)并将类设置为CustomTableViewCell并将其重用标识符设置为customCell

enter image description here enter image description here

接下来,在原型单元格中添加标签,并将其链接到CustomTableViewCell类中的IBOutlet。只要您在故事板中设置重用标识符,就不需要注册customCell。删除这一行:

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

它应该运行。

答案 1 :(得分:38)

试试这个

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
    }
}

答案 2 :(得分:7)

您应该使用dequeueReusableCellWithIdentifier:forIndexPath将单元格出列。

如果您使用故事板创建单元格,则不需要注册该类以供重用,因为如果您设置了reuseIdentifier,则故事板会为您执行此操作。

答案 3 :(得分:7)

像这样注册您的笔尖:

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

答案 4 :(得分:1)

在viewdidload中注册你的笔尖(自定义单元格)时,请确保没有任何错误。 Nib名称和reuseIdentifiers不应该是错误的。

答案 5 :(得分:-2)

我所做的一切都正确,但仍然出现此错误:

  • 在viewDidLoad中注册自定义笔尖。
  • 正确出队。
  • 断开并重新连接每个插座。

我终于通过重新创建新的xib解决了这个问题。 注意:您必须从头开始重新创建xib!原因是如果您将其复制将不起作用!

在我看来,这是一个XCode问题。

答案 6 :(得分:-5)

CustomTableViewCell中的Outlet必须具有强大的引用。

替换

@IBOutlet weak var titleLabel: UILabel! 

@IBOutlet var titleLabel: UILabel! 

答案 7 :(得分:-7)

没有为我工作!

修改

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

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