【问题标题】:unexpectedly found nil while unwrapping an Optional value in SWIFT 3.0在 SWIFT 3.0 中展开可选值时意外发现 nil
【发布时间】:2016-10-15 05:23:49
【问题描述】:

我在 swift 3.0 中使用带有自定义单元格的 tableview。我使用了一个 textview 并给出了 tabelview 单元格类的出口并在 tableview 委托方法中访问,但它向我显示了错误,例如 unexpectedly found nil while unwrapping an Optional value

这是我的代码。 记录是数组

查看此屏幕截图。仅供参考,我参加了一个新的自定义课程并为其提供了插座,但出现了同样的问题

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return record.count
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
   let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

  if let str = record.object(at: indexPath.row) as? String {
        print(cell.txtview)

            cell.txtview.text = str  

    }


    return cell
}

它在静态单元格中完美运行,但在自定义单元格中显示错误。

cell.textview 不是来自自定义单元格

请帮帮我

【问题讨论】:

  • 自定义单元格的标识符是什么,请检查是否有问题
  • 根据我的代码,我给了它相同的类和相同的标识符。但是标识符是作为字符串给出的,所以这不是问题
  • No @JAL 它在 siwft 3.0 中,如果它在 swift 2.0 或更低版本中,那么我已经解决了
  • 读取链接的副本。它与 Swift 语言版本无关。
  • @JAL 你不能给出重复的问题,因为它在 swift 2.0 和 swift 3.0 中有很多变化,你不,这个问题不是自定义单元格,它的 tableview 默认单元格。

标签: ios uitableview swift3


【解决方案1】:

请尝试以下行,不确定:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.register(TableViewCell.self, forCellReuseIdentifier: "TableViewCell")
         let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

        if let title = record.object(at: indexPath.row) as? String {
                cell.textview.text = title
        } else {
                cell.textview.text = ""
        }

        return cell
}

【讨论】:

  • 标题中的文字是什么? - 你能在这里发帖吗?
  • 我得到了这个“我在自定义 UITableViewCell 中有一个 UITextView。textview 委托在 tableviewcell 自定义类中分配。”从数组值
猜你喜欢
  • 2018-09-22
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多