在打开一个可选值swift时,意外地发现了nil [英] Unexpectedly found nil while unwrapping an Optional value swift
问题描述
我从Swift中打开 Optional
时收到错误消息意外发现 nil
下课。错误发生在该行:
I'm getting an error message "Unexpectedly found nil
while unwrapping an Optional
" from Swift, with the below class. The error occurs on the line:
(cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String
我有一个自定义单元类,其中2个UILabels标记为1和2,出口已设置
I have a custom cell class with 2 UILabels tagged 1 and 2, the outlets are set
import UIKit
import Foundation
class dictionaryTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
var objects = NSMutableArray()
var dataArray = [["firstName":"Debasis","lastName":"Das","email":"debasis_das@knowstack.com"],["firstName":"John","lastName":"Doe","email":"jdoe@knowstack.com"],["firstName":"Jane","lastName":"Doe","email":"janedoe@knowstack.com"],["firstName":"Mary","lastName":"Jane","email":"mjane@knowstack.com"]]
@IBOutlet
var tableView: UITableView!
var items: [String] = ["We", "Heart", "Swift"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count;//self.items.count;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
//cell.textLabel?.text = self.items[indexPath.row]
//return cell
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell
let object = dataArray[indexPath.row] as NSDictionary
(cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String
(cell.contentView.viewWithTag(2) as UILabel).text = object["lastName"] as? String
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
println("You selected cell #\(indexPath.row)!")
}
}
推荐答案
如果你想使用自定义单元格布局,我建议
If you want to use a custom cell layout, I would suggest
-
Subclass
UITableViewCell
:
// CustomTableViewCell.swift
import UIKit
class CustomTableViewCell: UITableViewCell {
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
}
在storyboard中创建表格视图。在storyboard中将单元格原型添加到tableview。设计单元格原型(添加您想要的任何标签)。
Create table view in storyboard. Add cell prototype to tableview in storyboard. Design that cell prototype (adding whatever labels you want).
指定单元原型的故事板标识符为您想要的任何内容(您的示例中的MyCell
。
Specify the storyboard identifier for the cell prototype to be whatever you want (MyCell
in your example).
指定单元原型的基类,在本例中为 CustomTableViewCell
:
Specify the base class for the cell prototype to be, in this example, CustomTableViewCell
:
在您的单元原型和 UITableViewCell之间连接
子类。 IBOutlet
引用
Hook up the IBOutlet
references between your cell prototype and your UITableViewCell
subclass.
注意 IBOutlet
引用左侧的实体项目符号。这表明该插座已正确连接。
Notice the solid bullets to the left of the IBOutlet
references. That indicates that the outlet was hooked up correctly.
在您的数据源中实施 cellForRowAtIndexPath
,例如在Swift 3中:
Implement cellForRowAtIndexPath
in your data source, e.g. in Swift 3:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath) as! CustomTableViewCell
let person = dataArray[indexPath.row]
cell.firstNameLabel.text = person["firstName"]
cell.lastNameLabel.text = person["lastName"]
return cell
}
或者Swift 2:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! CustomTableViewCell
let person = dataArray[indexPath.row]
cell.firstNameLabel.text = person["firstName"]
cell.lastNameLabel.text = person["lastName"]
return cell
}
注意,如果使用单元格原型,则不想在 viewDidLoad
中调用 registerClass
。故事板将自动为您重新注册您的自定义类(由于您在第3步和第4步中所做的操作)。
Note, if using cell prototypes, you do not want to call registerClass
in viewDidLoad
. The storyboard will automatically register your custom class with the reused identifier for you (because of what you have done in steps 3 and 4).
这篇关于在打开一个可选值swift时,意外地发现了nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!