ios 自定义表格视图单元格:IBOutlet 标签为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29100930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom table view cell: IBOutlet label is nil
提问by David Chouinard
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. Setting default properties of UITableViewCell
(like textLabel
) work just fine.
titleLabel
是零 - ?不清楚为什么。设置UITableViewCell
(like textLabel
) 的默认属性工作得很好。
I'm using a nib for the custom cell.
我正在为自定义单元使用笔尖。
Both the labels and table views are correctly connected to their IBOutlet
s.
标签和表格视图都正确连接到它们的IBOutlet
s。
Both the prototype cell and the custom nib view are marked as having a CustomTableViewCell
class.
原型单元格和自定义笔尖视图都被标记为有一个CustomTableViewCell
类。
I'm new to iOS development, am I missing something obvious?
我是 iOS 开发的新手,我是否遗漏了一些明显的东西?
回答by kellanburket
First off, you're using a nib file to load your custom cell into the table. That's probably going to be more of a headache than it's worth if you're new to 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 文件将自定义单元格加载到表格中。如果您是 Swift/Cocoa 的新手,这可能会更令人头疼,而不是值得。我会暂时将所有内容移至故事板。不要使用 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. You don't need to register your customCell
so long as you've set the reuse identifier in storyboard. Delete this line:
接下来,将标签添加到原型单元格并将其链接到CustomTableViewCell
类中的 IBOutlet 。customCell
只要您在故事板中设置了重用标识符,您就不需要注册。删除这一行:
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
and it should run.
它应该运行。
回答by LDNZh
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
}
}
回答by Arnav
Register your nib like this:
像这样注册你的笔尖:
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")
回答by Rory McKinnel
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.
如果您使用情节提要来创建单元格,则不需要注册类以供重用,因为如果设置了重用标识符,情节提要会为您完成。
回答by Narasimha Nallamsetty
Please make sure you are not doing any mistake while registering your nib(custom cell) in the viewdidload. Nib name and reuseIdentifiers should not be wrong.
请确保在 viewdidload 中注册您的笔尖(自定义单元格)时没有犯任何错误。笔尖名称和reuseIdentifiers 应该不会错。
回答by Cristina
The Outlet in your CustomTableViewCell must have strong reference.
您的 CustomTableViewCell 中的 Outlet 必须具有强引用。
Replace
代替
@IBOutlet weak var titleLabel: UILabel!
with
和
@IBOutlet var titleLabel: UILabel!
回答by iOSfleer
Works for me without !
没有!
EDIT
编辑
let challenge = self.challenges![indexPath.row]
let title = challenge["name"]
if title != nil {
cell.titleLabel.text = title
}