Custom UITableviewcell shows "fatal error: Can't unwrap Optional.None" issue in swift

Posted by user1656286 on Stack Overflow See other posts from Stack Overflow or by user1656286
Published on 2014-06-07T05:10:21Z Indexed on 2014/06/07 9:24 UTC
Read the original article Hit count: 381

Filed under:
|
|

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


}

enter image description here

When I run my code, I get the following error: "fatal error: Can't unwrap Optional.None" as seen in the image.

© Stack Overflow or respective owner

Related posts about ios

Related posts about swift-language