当我使用这个时,我的 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/