ios - Tableview 单元格字幕不显示或应用程序退出

标签 ios iphone swift uitableview tableview

当我使用这个时,我的 tableview 单元格字幕没有显示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell:UITableViewCell?

    if tableView.tag == 1 {

        guard let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") else {
            return UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
        }



        latestCell.textLabel?.text = latest[indexPath.row]

        latestCell.detailTextLabel?.text = latestSub[indexPath.row]

        latestCell.accessoryType = .disclosureIndicator

        return latestCell


    }
}

但是如果我使用这个:

else if tableView.tag == 2 {

        let olderCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "olderCell")



        olderCell.textLabel?.text = older[indexPath.row]

        olderCell.detailTextLabel?.text = olderSub[indexPath.row]

        olderCell.accessoryType = .disclosureIndicator

        return olderCell
    }

    else {
        return cell!
    }
}

字幕加载完美,但在我关闭应用程序并重新加载 View 后,应用程序自动退出,没有提供崩溃日志或将我带到调试选项卡。

我知道数据所来自的数组没有问题,而且我认为我已经在 Storyboard 中正确设置了所有内容。关于这个主题已经发布了很多类似的问题,但它们似乎都归结为忘记将 cellStyle 设置为 .subtitle。提前感谢我得到的任何帮助!

顺便说一句。我的常规单元格标题就像我希望的那样工作。没问题。

最佳答案

在您的第一部分中,在您设置单元格的文本和详细信息文本之前,您的 guard 语句将返回。如果您将其更改为:

if let latestCell = tableView.dequeueReusableCell(withIdentifier: "latestCell") {
    cell = latestCell            
} else {
    cell = UITableViewCell(style: .subtitle, reuseIdentifier: "latestCell")
}             
cell.textLabel?.text = latest[indexPath.row]

cell.detailTextLabel?.text = latestSub[indexPath.row]

cell.accessoryType = .disclosureIndicator

return cell

现在将设置标签。

而你的崩溃是由此引起的:

 return cell!

如果 cell == nil,则 cell!将尝试打开它。实际上,您应该做的是调用 super 的实现:

return super.tableView(tableView, cellForRowAt: indexPath)

祝你好运。

关于ios - Tableview 单元格字幕不显示或应用程序退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42321222/

相关文章:

ios - 在 Google Analytics 中合并两个 View 数据

android - 检测 phonegap 中的假/模拟位置?

iphone - 当应用程序处于前台和后台时区分推送通知处理程序

iphone - 如何在xcode中创建控件

ios - 在 Objective C 中调用 Swift 函数

ios - 无法本地化 iOS info.plist 文件中的字符串

ios - 如何将长按手势识别器添加到 tableview 单元格内的按钮?

javascript - iOS13 getUserMedia 不适用于 chrome 和 edge

arrays - 为什么我不能在 Swift 中将数组传递给可变参数?

ios - 标签不显示 Swift 中计时器的值