[英]Swift - Add tableview to a UIViewController
我正在迅速构建一个iOS应用程序。 我有一个UIViewController,它显示一些标签和字段,我想添加一个tableview。 我是一个快速的菜鸟,但是经过大量的搜索之后,我已经将所有东西集成在一起并且可以“正常工作”。 但是,当我使用dequeueReusableCellWithIdentifier时,遇到“致命错误:在展开可选值时意外发现nil”。 我的单元格是一个只有两个IBOutlet,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
}
确保您的IBOutlet
正确连接。 从视图中拖动到代码中的连接。
设置单元格标识符选择您的单元格,然后您将在Attributes Inspector
看到Identifier字段,在此处设置您的重用标识符...就是这样。
也可以尝试添加:-
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
并更换
let cell = tableView.dequeueReusableCellWithIdentifier("contactDetail") as! ContactDetailTableViewCell
至
let cell = tableView.dequeueReusableCellWithIdentifier("contactDetail", forIndexPath: indexPath) as! ContactDetailTableViewCell
说明
函数名称上的CMD + CLICK: 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)
您保证一个单元格。 也许这些链接是有帮助的:-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.