【问题标题】:Custom UITableviewcell shows "fatal error: Can't unwrap Optional.None" issue in swift自定义 UITableviewcell 快速显示“致命错误:无法解开 Optional.None”问题
【发布时间】:2014-06-07 05:10:21
【问题描述】:

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

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”,如图所示。

【问题讨论】:

  • @iPatel:这对我不起作用..我不需要创建 xib 文件..如果我创建 xib 文件,那么您的答案将有效。
  • 错误的堆栈跟踪是什么?

标签: ios swift xcode6


【解决方案1】:

嗨,我终于找到了我的问题的解决方案..请检查我的代码..它对我有用..

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

【讨论】:

  • 我为我找到的密钥没有使用“registerClass”。一旦我这样做了,它就会使用 NSCoder 而不是 CGRect 创建单元格。
  • 你改变了什么,老兄?
  • 我删除了注册并收到以下错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 TeamCell 的单元格出列 - 必须为标识符或连接故事板中的原型单元'
【解决方案2】:

第 1 步:在 viewDidLoad() 中注册 nib,如下所示

var xib : UINib = UINib (nibName: "WeatherTableViewCell", bundle: nil)
self.tableView.registerNib(xib, forCellReuseIdentifier: "Cell")

第 2 步:在 cellForRowIndexPath 中添加以下内容

var cell:WeatherTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? WeatherTableViewCell
cell!.weatherName.text = "weather" 
return cell

【讨论】:

    【解决方案3】:

    self.tableView.dequeueReusableCellWithIdentifier 只有在之前创建了一个单元格时才会返回一个单元格。如果它是 nil,您必须进行 nil 检查并创建一个新单元格。在你的情况下,它会在 cell.labelTitle.text = 上出错,因为 cell 可能在那里为零。

    【讨论】:

    • 感谢您的回复..我已经尝试过您的回答..但仍然是同样的问题..请查看我更新的问题...我已在 cellForRowAtIndexPath 中更新了我的问题..
    • 如果我将任何文本设置为 CustomTableViewCell 的“detailTextLabel”,那么它的工作..如果我尝试设置我自己的自定义标签(labelTitle),那么它会说错误..
    • 在 CustomTableViewCell 类 "@IBOutlet var labelTitle : UILabel" 中声明并使用 IBOutlet 连接的 labelTitle
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多