【问题标题】:fatal error: unexpectedly found nil while unwrapping an Optional value on table view cell致命错误:在表格视图单元格上展开可选值时意外发现 nil
【发布时间】:2014-12-11 09:04:52
【问题描述】:

在我的故事板中,我有 2 个UITabelViewControllers,它们与Tab Bar Controller 相连。这 2 个UITabelViewControllers 具有相同的行为。第一个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,但它有效。

提前谢谢你。

【问题讨论】:

标签: uitableview swift fatal-error


【解决方案1】:

我找到了原因,我忘记了一件重要的事情。

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

【讨论】:

  • 我已正确连接。 .还是有问题
【解决方案2】:

tableView.dequeueReusableCellWithIdentifier 可以返回 nil,但您在这里强制将其强制转换为非可选值:

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

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

您可以尝试使用as? 而不是as,然后检查cell 是否以nil 的形式返回,然后创建一个新单元格或在其他地方的代码中找到应该标记为用于重复使用的现有单元格不起作用。

【讨论】:

  • 如何创建具有相同行为的新单元格,以便单元格可以自行调整大小?可以给我写一些代码吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
  • 2016-02-29
  • 2020-09-19
  • 2016-01-08
相关资源
最近更新 更多