【发布时间】:2021-12-30 10:01:23
【问题描述】:
我正在使用表格视图,并且在其单元格中有一个标签。我想要的是在每次点击单元格时更改单元格标签。我知道 switch 语句不适用于 indexPath。我希望我的案例 1 和案例 2 能够正常工作。
这是我的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
switch Value {
case 1:
cell.cellLabel.text = "first time label changes"
case 2:
cell.cellLabel.text = "second time label changes"
default:
cell.cellLabel.text = "change the label"
}
}
有没有人有一个解决方案,如果我第一次点击一个单元格,案例 1 应该工作,如果我再次点击它,案例 2 应该工作?
【问题讨论】:
-
基本上修改
cellForRow之外的单元格是个坏主意,因为这些更改不是持久的。 -
澄清这一点:您误解了
indexPath是什么。它是单元格在列表中的位置,与交互无关,所以如果您选择单元格,它自然不会改变。在 TableView 中,IndexPath 由row和section组成,用于对您的内容进行分组
标签: ios swift xcode uitableview tableview