自定义UITableviewcell显示“致命错误:无法解包Optional.None”快速问题 [英] Custom UITableviewcell shows "fatal error: Can't unwrap Optional.None" issue in swift

查看:133
本文介绍了自定义UITableviewcell显示“致命错误:无法解包Optional.None”快速问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在UITableView中加载自定义单元格。我创建了一个名为CustomTableViewCell的UITableViewCell的自定义子类。我已经在桌面视图中添加了一个UITabelViewCell(使用拖放),如图所示。然后在文件检查器中,我将该UITabelViewCell的类设置为CustomTableViewCell。这是我的代码:

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
    if !cell
    {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Subtitle,
            reuseIdentifier: "CusTomCell")
    }
    println("cell \(cell)")
   // cell.?.labelTitle.text = items[indexPath.row]
    cell!.labelTitle.text = "some text"
    return cell
}





    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

当我运行我的代码时,我得到了以下错误:致命错误:无法打开Optional.None,如图所示。

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

推荐答案

嗨最后我找到我的问题的解决方案..请检查我的代码..对我有用..

Hi Finally i found solution for my question..please check my code..it works for me..

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet var tableView : UITableView

    var items = String[]()

    override func viewDidLoad() {
        super.viewDidLoad()
        items = ["Hi","Hello","How"]
        // 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", forIndexPath: indexPath) as? CustomTableViewCell
        cell!.labelTitle.text = "some text"
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

这篇关于自定义UITableviewcell显示“致命错误:无法解包Optional.None”快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆