致命错误:在表视图单元格中展开Optional值时意外发现nil

时间:2014-12-11 09:04:53

标签: uitableview swift fatal-error

在我的故事板中,我有2个UITabelViewController,并且它们与Tab Bar Controller相关联。这2 UITabelViewController具有相同的行为。第一个UITabelViewController工作正常,没有错误,但在第二个UITabelViewController我找到fatal error: unexpectedly found nil while unwrapping an Optional value

这是我的代码:

import UIKit

class PromotoreTVC: UITableViewController {

@IBOutlet weak var headerImage: UIImageView!

var dbManager = DBManager()
var company: Company!

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationController?.navigationBar.translucent = false
    self.tabBarController?.tabBar.translucent = false

    //save data from DB to arrays
    //connect to Sqlite
    self.dbManager.initWithDatabaseFilename("Exemple.sqlite")
    //select events and save them to arrays
    var query:NSString = "SELECT * FROM Company"
    var array:NSArray = self.dbManager.loadDataFromDB(query)
    println("array = \(array)")
    if (array.count != 0)
    {
        for (var i = 0; i < array.count; i++)
        {
            company = Company(companyId: array.objectAtIndex(i)[0].integerValue, name: array.objectAtIndex(i)[1] as String, logo: array.objectAtIndex(i)[2] as String, address: array.objectAtIndex(i)[3] as String, city: array.objectAtIndex(i)[4] as String, province: array.objectAtIndex(i)[5] as String, phone: array.objectAtIndex(i)[6] as String, fax: array.objectAtIndex(i)[7] as String, site: array.objectAtIndex(i)[8] as String, email: array.objectAtIndex(i)[9] as String, description: array.objectAtIndex(i)[10] as String)

        }
    }
    println("company name \(String(htmlEncodedString: company.name))") //here I can see that my company variable is filled
    var iconUrl:NSURL = NSURL.fileURLWithPath(company.logo)!
    var imageData:NSData = NSData(contentsOfURL: iconUrl)
    self.headerImage.image = UIImage(data: imageData) as UIImage

    // Remove extra separator
    self.tableView.tableFooterView = UIView(frame: CGRectZero)
    //custom separator
    self.tableView.separatorColor = UIColor.clearColor()

    // Self Sizing Cells
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
   return 8
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("PromoCell", forIndexPath: indexPath) as SchedaCompletaCellTVC

    // Configure the cell...
    switch indexPath.row {
    case 0:
        cell.valueLabel.text = String(htmlEncodedString: company.name) // HERE I HAVE ERROR!!!!!
        cell.valueLabel.font = UIFont(name: "AvenirNextCondensed-DemiBold", size:17.0)

    case 1:
        cell.valueLabel.text = String(htmlEncodedString: self.company.description)
    case 2:
        cell.valueLabel.text = "Contatti Aziendali"
        cell.valueLabel.font = UIFont(name: "AvenirNextCondensed-DemiBold", size:17.0)
    case 3:
        cell.valueLabel.text = "Indirizzo:  \(String(htmlEncodedString: company.address)),\(String(htmlEncodedString: company.city)),\(String(htmlEncodedString: company.province)) "
    case 4:
        cell.valueLabel.text = "Tel: \(String(htmlEncodedString: self.company.phone))"
    case 5:
        cell.valueLabel.text = "Email: \(String(htmlEncodedString: company.email))"
    case 6:
        cell.valueLabel.text = "Sito: \(String(htmlEncodedString: company.site))"
    case 7:
        cell.valueLabel.text = "Fax: \(String(htmlEncodedString: company.fax))"
    default:
        cell.valueLabel.text = ""

    }
    return cell
    }
}

这是我的tableviewcell类:

class SchedaCompletaCellTVC: UITableViewCell {

@IBOutlet weak var valueLabel:UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
    }

}

我不知道为什么cell.valueLabel为零。我在我的第一个tableviewcontroller中使用了相同的tableviewcell,但它确实有效。

提前感谢你。

2 个答案:

答案 0 :(得分:1)

我发现了原因,我忘记了一件必不可少的事情。

确保将引用出口:dataSource和委托连接到表视图。

答案 1 :(得分:0)

tableView.dequeueReusableCellWithIdentifier可以返回nil,但是你强制将它强制转换为非可选值:

let cell = tableView.dequeueReusableCellWithIdentifier("PromoCell", forIndexPath: indexPath) as SchedaCompletaCellTVC

如果您的可重复使用的单元格尚未正确标记,那么当as强制nil值变为SchedaCompletaCellTVC时,as?将会变为kaboom。

您可以尝试使用as代替cell,然后检查nil是否会以{{1}}的形式返回,然后创建新的小区或查找您的位置其他地方的代码应该标记现有的单元格以便重复使用它不起作用。