我正在swift中构建一个iOS应用程序。我有一个显示一些标签和字段的UIViewController,我想添加一个tableview。我是一个完全快速的菜鸟,但经过大量的谷歌搜索,我已经把所有的东西整合在一起并且工作了#34;正常。但是,我遇到了一个致命的错误:在解开一个Optional值时意外地发现了nil"当我使用dequeueReusableCellWithIdentifier时。我的单元格是一个自定义类,只有两个IBOutlets,detailKey和detailValue。有什么建议吗?
import UIKit
class ContactViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// MARK:Properties
var contact : Contact?
var params : [Detail]?
@IBOutlet weak var keywords: UILabel!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var contactDetails: UITableView!
// for use in params array
struct Detail {
var key : String,
value : String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
contactDetails.dataSource = self
contactDetails.delegate = self
if (self.contact != nil) {
// set default value for params array since optional
self.params = []
// make keywords string
var count : Int = 0
var keywordString : String = ""
while count < contact!.keywords.count {
keywordString += contact!.keywords[count].description
if count + 1 < contact!.keywords.count {
keywordString += ", "
}
count += 1
}
// compile details for tableview
if (!self.contact!.company.isEmpty) { self.params! += [Detail(key: "Company", value: self.contact!.company)] } // company
if (!self.contact!.phone1.isEmpty) {
self.params! += [Detail(key: self.contact!.phone_label1.isEmpty ? "Phone 1" : self.contact!.phone_label1, value: self.contact!.phone1)]
}
if (!self.contact!.phone2.isEmpty) {
self.params! += [Detail(key: self.contact!.phone_label2.isEmpty ? "Phone 2" : self.contact!.phone_label2, value: self.contact!.phone2)]
}
if (!self.contact!.email1.isEmpty) {
self.params! += [Detail(key: self.contact!.email_label1.isEmpty ? "Email 1" : self.contact!.email_label1, value: self.contact!.email1)]
}
if (!self.contact!.email2.isEmpty) {
self.params! += [Detail(key: self.contact!.email_label2.isEmpty ? self.contact!.email_label2 : "Email 2", value: self.contact!.email2)]
}
if (!self.contact!.street.isEmpty) { self.params! += [Detail(key: "Address", value: self.contact!.address)] } // address (condition checks for street)
if (!self.contact!.dob.isEmpty) { self.params! += [Detail(key: "Birthday", value: self.contact!.dob)] } // dob
if (!self.contact!.social.isEmpty) { self.params! += [Detail(key: "Social Security Number", value: self.contact!.social)] } // social
self.name.text = contact!.name
self.keywords.text = keywordString
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// table view stuffs
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.params!.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("going")
let cell = tableView.dequeueReusableCellWithIdentifier("contactDetail") as! ContactDetailTableViewCell
let param = self.params![indexPath.row]
print(param.key + ": " + param.value)
cell.detailKey.text = param.key
cell.detailValue.text = param.value
return cell
}
答案 0 :(得分:3)
确保您的IBOutlet
已正确连接。从视图拖到代码中的连接。
答案 1 :(得分:3)
设置您的小区标识符选择您的小区,然后您会在Attributes Inspector
中看到标识符字段,在那里设置您的重用标识符...这就是它......
还尝试添加: -
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
并替换
let cell = tableView.dequeueReusableCellWithIdentifier("contactDetail") as! ContactDetailTableViewCell
到
let cell = tableView.dequeueReusableCellWithIdentifier("contactDetail", forIndexPath: indexPath) as! ContactDetailTableViewCell
<强>说明强>
CMD +点击功能名称:dequeueReusableCellWithIdentifier
-
您将被引导至其文档..
public func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell? // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
@available(iOS 6.0, *)
public func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath) -> UITableViewCell // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
dequeueReusableCellWithIdentifier(identifier: String, forIndexPath indexPath: NSIndexPath)
保证你有一个牢房.. 也许这些链接很有帮助: -
Fatal error: unexpectedly found nil while unwrapping an Optional values