swift - swift中表格视图中的单个单元格选择
问题描述
我有一个表格视图,其中有两个部分。两个部分都包含 3 个单元格。现在,当我选择任何单元格时,它会显示勾号,但它会从两个部分中选择一个单元格。我尝试了一些代码,但它不起作用。我希望用户可以从两个部分中选择一个单元格,并且当表格视图加载时,它的第一个单元格应该被预先选择。我怎样才能做到这一点?我对单元格的预选有点困惑。我的单元格选择代码是这样的,
self.filterTableView.allowsSelection = true
extension FiltersVC: UITableViewDelegate,UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitles[section]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems[section].count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = filterTableView.dequeueReusableCell(withIdentifier: "filterCell", for: indexPath) as! FiltersTableViewCell
cell.tilteLbl.text = menuItems[indexPath.section][indexPath.row]
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none
cell.backgroundColor = UIColor.clear
return cell
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView = UIView()
headerView.backgroundColor = #colorLiteral(red: 0.9130856497, green: 0.9221261017, blue: 0.9221261017, alpha: 1)
let headerText = UILabel()
headerText.textColor = UIColor.black
headerText.adjustsFontSizeToFitWidth = true
switch section{
case 0:
headerText.textAlignment = .center
headerText.text = "LIST BY"
headerText.backgroundColor = #colorLiteral(red: 0.9190355449, green: 0.9281349067, blue: 0.9281349067, alpha: 1)
headerText.font = UIFont.boldSystemFont(ofSize: 20)
case 1:
headerText.textAlignment = .center
headerText.text = "COUSINE"
headerText.backgroundColor = #colorLiteral(red: 0.921431005, green: 0.9214526415, blue: 0.9214410186, alpha: 1)
headerText.font = UIFont.boldSystemFont(ofSize: 20)
default: break
}
return headerText
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if let cell = filterTableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "listBy")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
else if indexPath.section == 1{
if let cell = filterTableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
let item = menuItems[indexPath.section][indexPath.row]
UserDefaults.standard.set(item, forKey: "Cuisine")
UserDefaults.standard.synchronize()
filterBtn.isUserInteractionEnabled = true
filterBtn.backgroundColor = #colorLiteral(red: 0.9529120326, green: 0.3879342079, blue: 0.09117665142, alpha: 1)
}
}
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if indexPath.section == 0 {
if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
}
}
else if indexPath.section == 1{
if let cell = filterTableView.cellForRow(at: indexPath as IndexPath) {
cell.accessoryType = .none
}
}
}
解决方案
您必须将选定的索引保存在某处,可能在某些具有不同部分的数组中。由于您希望预先选择每个部分的第一个单元格,因此可以从以下内容开始:
var selectedIndexes = [[IndexPath.init(row: 0, section: 0)], [IndexPath.init(row: 0, section: 1)]]
在上面的数组中,我们保存了两个索引路径。一个用于第一部分的第一个单元格,第二个用于第二部分的第一个单元格。
现在您的 cellForRow 可以像这样检查数组中的现有索引路径:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! UITableViewCell
cell.selectionStyle = .none
cell.textLabel?.text = tableArray[indexPath.section][indexPath.row]
let selectedSectionIndexes = self.selectedIndexes[indexPath.section]
if selectedSectionIndexes.contains(indexPath) {
cell.accessoryType = .checkmark
}
else {
cell.accessoryType = .none
}
return cell
}
对于单选:
// For single selection
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
// If current cell is not present in selectedIndexes
if !self.selectedIndexes[indexPath.section].contains(indexPath) {
// mark it checked
cell?.accessoryType = .checkmark
// Remove any previous selected indexpath
self.selectedIndexes[indexPath.section].removeAll()
// add currently selected indexpath
self.selectedIndexes[indexPath.section].append(indexPath)
tableView.reloadData()
}
}
上面的代码删除任何以前选择的单元格并保存新的单元格。如果一次又一次地选择相同的单元格,它仍然处于选中状态。
推荐阅读
- asp.net-mvc-3 - 在 HTTP post 提交 MVC 3 后显示“确认”选项卡
- python - 导入 Bazel 构建 TensorFlow
- coldfusion - Coldfusion电子表格宽度限制
- css - 如何更改 Bootstrap 4 中禁用选项卡的样式?
- ember.js - ember 验收测试等待助手
- python - 从两个数据框访问行元素作为复合列表
- python - 如何创建一个迭代列表,其中一些变量是独立的,一些是依赖的?
- angular - 两个组件之间通过服务进行的通信不起作用
- r - 使用 grepl 为数据框创建函数
- python - 解析子文件夹中的文件