自定义 table 视图单元格:IBOutlet 标签为零
Custom table view cell: IBOutlet label is nil
考虑以下简单的视图控制器:
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!
}
此代码导致以下错误。
fatal error: unexpectedly found nil while unwrapping an Optional value
titleLabel
为零——原因不明。设置 UITableViewCell
的默认属性(如 textLabel
)工作正常。
我正在为自定义单元格使用笔尖。
标签和 table 视图都正确连接到它们的 IBOutlet
。
原型单元格和自定义 nib 视图都被标记为具有 CustomTableViewCell
class.
我是 iOS 开发的新手,我是否遗漏了一些明显的东西?
首先,您使用 nib 文件将自定义单元格加载到 table。如果您是 Swift/Cocoa 的新手,那么这可能会让人头疼,而不是值得的。我会暂时将所有内容移至故事板。不要使用 nib 文件单击,而是转到故事板,单击您的 UITableView
并确保 TableView 的内容设置为 Dyanamic Prototypes
:
接下来,单击原型单元格(table 视图中唯一的单元格)并将 class 设置为 CustomTableViewCell
并将其重用标识符设置为 customCell
:
接下来,将标签添加到您的原型单元格,并将其 link 添加到您 CustomTableViewCell
class 中的 IBOutlet。只要您在情节提要中设置了重用标识符,就无需注册 customCell
。删除此行:
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
应该运行.
您应该使用 dequeueReusableCellWithIdentifier:forIndexPath
使单元格出队。
如果您使用情节提要来创建单元格,您不需要注册 class 以进行重用,因为如果您设置了 reuseIdentifier,情节提要会为您完成。
没有 !
对我有用
编辑
let challenge = self.challenges![indexPath.row]
let title = challenge["name"]
if title != nil {
cell.titleLabel.text = title
}
试试这个
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
}
}
请确保您在 viewdidload 中注册您的 nib(自定义单元格)时没有犯任何错误。 nib名称和reuseIdentifiers应该不会错
像这样注册你的笔尖:
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")
考虑以下简单的视图控制器:
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!
}
此代码导致以下错误。
fatal error: unexpectedly found nil while unwrapping an Optional value
titleLabel
为零——原因不明。设置 UITableViewCell
的默认属性(如 textLabel
)工作正常。
我正在为自定义单元格使用笔尖。
标签和 table 视图都正确连接到它们的 IBOutlet
。
原型单元格和自定义 nib 视图都被标记为具有 CustomTableViewCell
class.
我是 iOS 开发的新手,我是否遗漏了一些明显的东西?
首先,您使用 nib 文件将自定义单元格加载到 table。如果您是 Swift/Cocoa 的新手,那么这可能会让人头疼,而不是值得的。我会暂时将所有内容移至故事板。不要使用 nib 文件单击,而是转到故事板,单击您的 UITableView
并确保 TableView 的内容设置为 Dyanamic Prototypes
:
接下来,单击原型单元格(table 视图中唯一的单元格)并将 class 设置为 CustomTableViewCell
并将其重用标识符设置为 customCell
:
接下来,将标签添加到您的原型单元格,并将其 link 添加到您 CustomTableViewCell
class 中的 IBOutlet。只要您在情节提要中设置了重用标识符,就无需注册 customCell
。删除此行:
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "customCell")
应该运行.
您应该使用 dequeueReusableCellWithIdentifier:forIndexPath
使单元格出队。
如果您使用情节提要来创建单元格,您不需要注册 class 以进行重用,因为如果您设置了 reuseIdentifier,情节提要会为您完成。
没有 !
对我有用编辑
let challenge = self.challenges![indexPath.row]
let title = challenge["name"]
if title != nil {
cell.titleLabel.text = title
}
试试这个
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
}
}
请确保您在 viewdidload 中注册您的 nib(自定义单元格)时没有犯任何错误。 nib名称和reuseIdentifiers应该不会错
像这样注册你的笔尖:
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "PickerCell", bundle: bundle)
collectionViewPicker.register(nib, forCellWithReuseIdentifier: "cell")