HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Swift UITableViewCell detailTextLabel.text抛出错误’致命错误:无法解包Optional.None’大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我生成表格视图的 Swift代码.我正在尝试设置带有详细标签的tableView.我相信问题的产生是因
if (cell == nil) {
            println("1")
            cell = UITableViewCell(style: .Subtitle,reusEIDentifier: "CellSubtitle")
            //cell = tableViewCell
        }

永远不会被调用,因此永远不会使用UITableViewCellStyle.Subtitle样式初始化单元格.以下是该方法所需的代码

func tableView(tableView: UITableView!,cellForRowATindexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
    println("start TableViewCellForRowATindexPath")
    var cell: UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("CellSubtitle") as UITableViewCell
    if (cell == nil) {
        println("1")
        cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reusEIDentifier: "CellSubtitle")
        //cell = tableViewCell
    }

    cell.textLabel.text = instructions[indexPath.row].text
    println("2")
    //cell.detailTextLabel
    cell.detailTextLabel.text = "HI"
    println("3")

以下是该方法的控制台输出

start load
1
2
done load
start TableViewCellForRowATindexPath
2
Fatal error: Can't unwrap Optional.None
(lldb)

如何初始化detailTextLabel以插入文本?当我尝试设置标签的文本时,我收到致命错误:无法打开Optional.None.为什么我收到此错误

单元格不是在故事板中创建的.我使用tableView.registerClass初始化单元格或注册其类(UITableViewCell.self,forCellReusEIDentifier:“CellSubtitle”)

解决方法

我假设你在故事板中创建了你的单元格,这就是为什么永远不会调用“if”子句的原因.您只需要在故事板的检查器中将单元格的样式更改为“Subtitle”(并删除该if子句).

大佬总结

以上是大佬教程为你收集整理的ios – Swift UITableViewCell detailTextLabel.text抛出错误’致命错误:无法解包Optional.None’全部内容,希望文章能够帮你解决ios – Swift UITableViewCell detailTextLabel.text抛出错误’致命错误:无法解包Optional.None’所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。