In my storyboard, I have 2 UITabelViewController
s and they are connected with a Tab Bar Controller
. This 2 UITabelViewController
s 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.