uitableview - fatal error : unexpectedly found nil while unwrapping an Optional value on table view cell

标签 uitableview swift fatal-error

在我的 Storyboard中,我有 2 个 UITabelViewController,它们与一个 Tab Bar Controller 相连。这 2 个 UITabelViewController 具有相同的行为。第一个 UITabelViewController 工作正常,没有错误,但在第二个 UITabelViewController 中我发现了 fatal error: unwrappedly 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,但它起作用了。

在此先感谢您。

最佳答案

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

确保连接引用导出:数据源并委托(delegate)给 TableView 。

关于uitableview - fatal error : unexpectedly found nil while unwrapping an Optional value on table view cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27418933/

相关文章:

ios - 无法以编程方式选择表格 View 单元格

iOS8 实现具有动态高度的 heightForRowAtIndexPath

iOS 自动布局 : Align left edge of a UILabel with horizontal center in container?

swift - 协议(protocol)子类实现

swift - 创建一个继承自UITouch的类

magento - 升级到 Magento 1.9 后的 fatal error

undefined - fatal error :在第1行的index.php中调用未定义的函数get_header()

ios - 当按下 UITableView 行时正确触发 View Controller 打开的问题?

ios - Swift 中关联类型的协议(protocol)问题

dart - flutter 错误 : Document references must have an even number of segments