【发布时间】:2019-10-11 06:19:01
【问题描述】:
UITableView 有多个 Sections 和多行。所有行都添加了复选标记附件,我需要每个部分的行为不同。一个部分只允许选择 1 个复选标记,而另一个部分允许多选。所以目前我的第一部分只允许选择一行。 When another row in that section is selected it deselects the other.
在我的第二部分中,用户可以选择多个选项,这也是有效的,但是这是我的问题所在。当我在“第 2 节”中选择一行时,它会取消选择“第 1 节”中选择的选项。我正在使用一堆 switch 语句试图让它工作,但感觉必须有一个更简单的方法。有任何想法吗? (请在下面找到代码)
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let sectionNumber = indexPath.section
switch sectionNumber {
case 0:
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
case 1:
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark {
cell.accessoryType = .none
} else {
cell.accessoryType = .checkmark
}
}
case 2:
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark {
cell.accessoryType = .none
} else {
cell.accessoryType = .checkmark
}
}
case 3:
if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
if cell.accessoryType == .checkmark {
cell.accessoryType = .none
} else {
cell.accessoryType = .checkmark
}
}
default:
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let sectionNumber = indexPath.section
let rowNumber = indexPath.row
switch sectionNumber {
case 0:
tableView.cellForRow(at: indexPath)?.accessoryType = .none
case 1:break
case 2: break
case 3:break
default:break
}
【问题讨论】:
标签: swift xcode uitableview