ios - UITableView Multi Selection 的选中复选标记未保持选中状态
问题描述
我的申请中有两个UITableView
。一个用于类别,第二个用于子类别。在选择 Category SubCategory 的基础上UITableView
,数据会发生变化,并且 SubCategoryUITableView
具有多选功能,直到我的应用程序运行正常。
现在的问题是,当我在类别上UITableView
并单击假设类别单元格时,它将重定向到各个子类别,在该屏幕上,我选择了多个选项并单击返回按钮出现在顶部,当我再次单击类别选项卡时,我的选择(复选标记)正在消失。
只要我手动将它们设置为未选中,我就希望选中我的复选标记。
我怎样才能实现那个东西?
下面附上我的应用程序的示例屏幕截图。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tblSubCategory.cellForRow(at: indexPath)
if cell!.isSelected
{
cell!.isSelected = false
if cell!.accessoryType == UITableViewCell.AccessoryType.none
{
if strCategoryData == "Category" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedCetegoryIndex.append(objectForCell)
let defaults = UserDefaults.standard
defaults.set(arrSelectedCetegoryIndex, forKey: "categoryKey")
}
else if strCategoryData == "Brand" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedBrandIndex.append(objectForCell)
}
else if strCategoryData == "Color" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedColorIndex.append(objectForCell)
}
else if strCategoryData == "Size" {
cell!.accessoryType = UITableViewCell.AccessoryType.checkmark
let objectForCell = arrSubCategoryData[indexPath.row]
arrSelectedSizeIndex.append(objectForCell)
}
}
else
{
if strCategoryData == "Category" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedCetegoryIndex.firstIndex(of: selectedIndexValue)!
arrSelectedCetegoryIndex.remove(at: index)
}
else if strCategoryData == "Brand" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedBrandIndex.firstIndex(of: selectedIndexValue)!
arrSelectedBrandIndex.remove(at: index)
}
else if strCategoryData == "Color" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedColorIndex.firstIndex(of: selectedIndexValue)!
arrSelectedColorIndex.remove(at: index)
}
else if strCategoryData == "Size" {
cell!.accessoryType = UITableViewCell.AccessoryType.none
let selectedIndex = (tblSubCategory.indexPathForSelectedRow?.row)!
let selectedIndexValue = arrSubCategoryData[selectedIndex]
print(selectedIndexValue)
let index = arrSelectedSizeIndex.firstIndex(of: selectedIndexValue)!
arrSelectedSizeIndex.remove(at: index)
}
}
}
}
解决方案
您需要创建一个 Int 类型数组,然后在单击时附加值(如果不在数组中并且如果已经存在,则需要从数组中删除并在 cellForRowAt 方法中设置复选标记)。
请查看完整代码
import UIKit
class testViewController: UIViewController {
var selectedRows: [Int] = []
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension testViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
cell.textLabel?.text = "Welcome " + (indexPath.row+1).description
cell.selectionStyle = .none
cell.accessoryType = selectedRows.contains(indexPath.row) ? .checkmark : .none
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.selectedRows.contains(indexPath.row) {
if let index = self.selectedRows.firstIndex(of: indexPath.row) {
self.selectedRows.remove(at: index)
}
} else {
self.selectedRows.append(indexPath.row)
}
tableView.reloadData()
}
}
推荐阅读
- excel - 将数据粘贴到工作表时设置范围
- maven - Maven 拉入旧版本的库
- jquery-ui - JQuery UI 自动完成可滚动内容在鼠标移动时向上滚动
- windows - 根据条件使用 Powershell 在 xml 中附加属性
- html - 我可以将函数注入 HTML 字符串吗?
- string - 唯一的自定义字符串哈希
- reactjs - 在 React 中写入文件
- powershell - 如何从下拉列表中选择当前工作班次 - PowerShell / Selenium / Chrome
- r - GGplot2:如何避免散点图输出模糊和文本模糊?
- ios - 您可以创建一个独立的 iOS 手表应用程序 *WITH* NFC 吗?