我有一个表格视图与静态内容,我试图访问所选行的标签文本,但它由于某种原因返回nil。什么可能导致它?label.text为表格单元格返回无(swift)
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let currentCell = tableView.cellForRow(at: indexPath) as UITableViewCell?
let label = currentCell!.textLabel
if label != nil {
let text = label!.text
NSLog(text ?? "Label text is nil")
} else {
NSLog("Label is nil")
}
}
在我的例子我看到标签文本是零
如果tableview有静态内容,为什么你需要访问标签文本?你不能只访问填充你的tableview的数组中的数据吗?就像'let text = labelTextArray [indexPath.row]' –
@KaylaGalway,因为该表没有数据 - 它是从IB静态的。 。 Elena,你确定你正在访问正确的标签吗?也许你的文本是在其他标签上,比如'detailTextLabel'或者其他你手动添加的标签? – jovanjovanovic
@jovanjovanovic,问题是在单元格的样式(这是自定义),我改变它后,我得到了文字 –