我从
Swift获得了一个错误消息“在unjrapping一个Optional时意外发现nil”,下面的类.该行发生错误:
(cell.contentView.viewWithTag(1) as UILabel).text = object["firstName"] as? String
我有一个自定义的单元类,其中2个UILabels标记为1和2,插座已设置
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","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)!") } }
解决方法
如果你想使用自定义单元格布局,我建议
>子类UITableViewCell:
// CustomTableViewCell.swift import UIKit class CustomTableViewCell: UITableViewCell { @IBOutlet weak var firstNameLabel: UILabel! @IBOutlet weak var lastNameLabel: UILabel! }
>在故事板中创建表格视图.在storyboard中将单元格原型添加到tableview.设计细胞原型(添加你想要的任何标签).
>将单元格原型的故事板标识符指定为您想要的任何内容(在您的示例中为MyCell).
>在此示例中,指定单元原型的基类CustomTableViewCell:
>在您的单元原型和UITableViewCell子类之间连接IBOutlet引用.
请注意IBOutlet参考左侧的实体项目符号.这表明插座正确连接.
>在数据源中实现cellForRowAtIndexPath,例如在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中所做的操作).