首页 > 解决方案 > 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)
            }
        }
    }
}

标签: iosswiftuitableviewswift4.2

解决方案


您需要创建一个 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()
    }
}


推荐阅读