Reputation: 1219
I have a ViewController and a TableViewController on the storyboard. The TableViewController has a custom cell with a label on it. Custom cell has its own class CompanyTableViewCell.swift:
import UIKit
class CompanyTableViewCell: UITableViewCell {
@IBOutlet var companyName : UILabel
init(style: UITableViewCellStyle, reuseIdentifier: String) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
println("Cell's initialised")
}
... // other default cell methods
}
In my TableViewController I have methods:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(CompanyTableViewCell.self, forCellReuseIdentifier: "CompanyCell")
println("Cell's registered")
}
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {
let cell = tableView.dequeueReusableCellWithIdentifier("CompanyCell") as CompanyTableViewCell
println(cell.companyName)
cell.companyName.text = "Company name"
return cell
}
When I run the code, if the TableViewController is marked as initial view controller it runs ok and the text of the label on my custom cell is being changed successfully. I can see bunch of cells with Company name
.
But when I assign initial view controller property to the ViewController, and trying to access the TableViewController from it as (the ViewController has a button which runs searchByName action):
// ViewController.swift
@IBAction func searchByName(sender : UIButton) {
var vc = SearchTableViewController(style: UITableViewStyle.Plain)
vc.companies = self.companies
self.showViewController(vc, sender: self)
}
I've got app crush on cell.companyName.text = "Company name"
string with error: fatal error: Can't unwrap Optional.None. On the previous string println(cell.companyName)
prints nil to console, although in console I see that cell class was registered and cell object was initialised.
The question is how to assign text to the label, what I've forgotten? And why it works in the first case but not in the second. Thanks.
Upvotes: 1
Views: 1367
Reputation: 10432
@IBOutlet var companyName : UILabel
is an outlet , if you call var vc = SearchTableViewController(style: UITableViewStyle.Plain)
The outlet companyName label is not initialised , therefore it is nil , therefore accessing companyName label causes crash.
But in the first case you are initialising viewController from storyboard which is having SearchTableViewController scene contains CompanyTableViewCell with outlet companyName and it is connected to Cell ,hence it is initialised , there fore it works as expected.
Solution is :
If your are using storyBoard use instantiateViewControllerWithIdentifier:
method to initialise viewController(assuming that SearchTableViewController has CompanyTableViewCell in the storyboard).
If you are doing programatically then override all initialiser method of CompanyTableViewCell to create a label and add it to cell.
Upvotes: 3
Reputation: 76918
cell.companyName
is an implicitly unwrapped optional and it is nil
(not a UILabel
)
The error is saying that you can't assign to (nil).text
I don't know how your custom cell works but you may be able to assign a UILabel
instance to cell.companyName
Upvotes: 1