Custom UITableviewcell shows "fatal error: Can't unwrap Optional.None" issue in swift
- by user1656286
I need to load a custom cell in a UITableView. I created a custom subclass of UITableViewCell named "CustomTableViewCell". I have added a UITabelViewCell to the tableview (using drag and drop) as shown in figure. Then in file inspector I set the class of that UITabelViewCell to be "CustomTableViewCell". Here is my code:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView : UITableView
var items = String[]()
override func viewDidLoad() {
super.viewDidLoad()
items = ["Hi","Hello","How"]
self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CusTomCell")
// Do any additional setup after loading the view, typically from a nib.
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return items.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
var cell:CustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell") as CustomTableViewCell
cell.labelTitle.text = items[indexPath.row]
return cell;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I run my code, I get the following error: "fatal error: Can't unwrap Optional.None" as seen in the image.