【发布时间】:2017-02-07 11:52:09
【问题描述】:
我是 Swift 新手,我想在我的表格视图中禁用多行选择。我尝试实现不同类型的方法,我的最终代码块如下,这是行不通的。 感谢您的帮助。
override func viewDidLoad() {
super.viewDidLoad()
companyTableView.allowsMultipleSelection = false
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark;
}
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
tableView.deselectRow(at: indexPath, animated: true)
if let cell = tableView.cellForRow(at: indexPath) {
if selectedCells.contains(indexPath.row) {
selectedCells.remove(indexPath.row)
cell.accessoryType = .none
} else {
selectedCells.insert(indexPath.row)
cell.accessoryType = .checkmark
}
}
}
【问题讨论】:
-
allowsMultipleSelection = false工作正常。 -
默认情况下,单选仅在情节提要中被选中。在情节提要中-> 选择您的表格视图,在属性检查器下有一个选择选项:否、单选、多选。如果您不想在运行时更改它,可以从情节提要中进行设置。
allowsMultipleSelection的默认值也是 false。 -
显示
cellForRowAtindexPath代码。 -
Priyal,我已经在 Storyboard 中选择了“单选”,但它并没有解决我的问题。
标签: ios swift xcode uitableview tableview