我有一个包含不同部分的静态 TableView 。您可以在此处看到其中一个部分:
用户可以点击这些部分中的单元格来检查它们,但我希望他们不能点击同一部分中的两个单元格,例如,如果他们点击了名为“Name A-Z”的单元格,那么他们点击名为“Name Z-A”的,第一个未被选中,第二个被选中。为此,我认为,我应该以某种方式检查两个单元是否来自同一部分,但我不知道如何实现。我正在使用
tableView(_ didSelectRowAt:)
选择单元格的方法,但我不知道如何从那里访问单元格的部分。也许我应该使用另一种方法?有什么想法如何实现这个吗?
最佳答案
好的,下面的代码可以让您在选择单元格时执行特定操作。代码也会自动识别它所在的部分。
当前,该表更新标签以表示已选择,并自动将其他两个设置为 N/A。您可以将其替换为您自己的代码以隐藏/显示复选标记或其他内容。
Here is a video下面的代码也做了什么。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
sectionOneCellOne.textLabel?.text = "SELECTED"
sectionOneCellTwo.textLabel?.text = "N/A"
sectionOneCellThree.textLabel?.text = "N/A"
case 1:
sectionOneCellOne.textLabel?.text = "N/A"
sectionOneCellTwo.textLabel?.text = "SELECTED"
sectionOneCellThree.textLabel?.text = "N/A"
case 2:
sectionOneCellOne.textLabel?.text = "N/A"
sectionOneCellTwo.textLabel?.text = "N/A"
sectionOneCellThree.textLabel?.text = "SELECTED"
default:
break
}
case 1:
switch indexPath.row {
case 0:
sectionTwoCellOne.textLabel?.text = "SELECTED"
sectionTwoCellTwo.textLabel?.text = "N/A"
sectionTwoCellThree.textLabel?.text = "N/A"
case 1:
sectionTwoCellOne.textLabel?.text = "N/A"
sectionTwoCellTwo.textLabel?.text = "SELECTED"
sectionTwoCellThree.textLabel?.text = "N/A"
case 2:
sectionTwoCellOne.textLabel?.text = "N/A"
sectionTwoCellTwo.textLabel?.text = "N/A"
sectionTwoCellThree.textLabel?.text = "SELECTED"
default:
break
}
default:
break
}
}
sectionOneCellOne
变量等只是在 View Controller 中定义的单个单元格。
关于ios - 如何找出静态 TableView 单元格属于哪个部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47042367/