1

In my storyboard, I have 2 UITabelViewControllers and they are connected with a Tab Bar Controller. This 2 UITabelViewControllers have the same behavior. The first UITabelViewController works fine, it has NO errors, but in the second UITabelViewController I found fatal error: unexpectedly found nil while unwrapping an Optional value.

Here is my code:

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
    }
}

This is my tableviewcell class:

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
    }

}

I have no idea why cell.valueLabel is nil. I used the same tableviewcell in my first tableviewcontroller, but it worked.

Thanking you in advance.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Chongzl
  • 589
  • 1
  • 9
  • 25
  • Cell is not unwrapped properly. Have a look on this - http://stackoverflow.com/questions/24166662/swift-tableview-for-ios/26888012#26888012 – Kampai Dec 11 '14 at 11:05
  • I read the post before, but I still can't understand how I can fix it. – Chongzl Dec 11 '14 at 11:35

2 Answers2

1

I found out why, I've forgot one essential thing.

Make sure to connect the referencing outlets: dataSource and delegate to the Table View.

Chongzl
  • 589
  • 1
  • 9
  • 25
0

tableView.dequeueReusableCellWithIdentifier can return a nil, but you are force-casting it to a non-optional value here:

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

If your reusable cell hasn’t been properly marked already, then that as will go kaboom when it forces a nil value to become a SchedaCompletaCellTVC.

You could try using as? instead of as, and then checking if cell is coming back as nil, then either creating a new cell or finding where in your code elsewhere that ought to be marking an existing cell for reuse it isn’t working.

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
  • how can I create a new cell with the same behavior, so the cells can self sizing? Can you write me some code? – Chongzl Dec 11 '14 at 15:12