【问题标题】:UITableView Multi Selection's selected checkmark not remains checkedUITableView Multi Selection 的选中复选标记未保持选中状态
【发布时间】:2019-06-22 07:06:29
【问题描述】:

我的应用程序中有两个UITableView。 一个用于类别,第二个用于子类别。 在选定类别子类别UITableView的基础上,数据会发生变化,子类别UITableView具有多选功能,直到我的应用程序运行良好。

现在的问题是,当我在类别 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)
            }
        }
    }
}

【问题讨论】:

  • 你能分享一些你是如何管理这一切的代码吗?我认为你必须通过存储选定的值来通过数组来管理它。
  • 正确。当每次我将其值附加到数组时选择子类别单元格时
  • @TejasPatel - 您是否在检查 cellForRowAt 方法中的选定值数组?
  • cellForRowAt indexPath方法的分享码
  • @UdayBabariya 用 didselectrowatindex 路径方法代码更新了我的问题

标签: ios swift uitableview swift4.2


【解决方案1】:

您可能正在执行转场以转到子类别视图控制器,并且每次执行此转场时,都会再次调用 tableview 委托和数据源方法并重新初始化单元格。

为了显示您的单元格已被选中,您需要将选中的值保存在 Categories 视图控制器中,并将它们传递给 SubCategory 视图控制器,并在 cellForRowAtIndexpath 方法中设置选中的值。

这是一个如何实现的示例:

class CategoryViewController: UIViewController {

    var checkedValues = [[Bool]]()
    var indexSelected = -1

    override func viewDidLoad() {
        super.viewDidLoad()


        // your code here
        checkedValues.append(contentsOf: repeatElement([], count: yourCategArray.count))


    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // your code here
        indexSelected = indexPath.row
        self.performSegue(withIdentifier: "yourSegueIdentifierHere", sender: self)
    }    


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        (segue.destination as! SubCategoryViewController).parentCategoryVC = self

    }

}

现在是另一个视图控制器:

class SubCategoryViewController: UIViewController {

    var parentCategoryVC = CategoryViewController()


    override func viewDidLoad() {
        super.viewDidLoad()

        if parentCategoryVC.checkedValues[parentCategoryVC.indexSelected].count == 0 {
            parentCategoryVC.checkedValues[parentCategoryVC.indexSelected].append(contentsOf: repeatElement(false, count: yourSubCategArray.count))

        }

        // your code here

    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return yourSubCategArray.count
   }





    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell...

        if parentCategoryVC.checkedValues[parentCategoryVC.indexSelected][indexPath.row] { cell.accessoryType = .checkmark } else { cell.accessoryType = .none }

        // your code here

        return cell

    }


        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            // your code 
            parentCategoryVC.checkedValues[parentCategoryVC.indexSelected][indexPath.row] = !parentCategoryVC.checkedValues[parentCategoryVC.indexSelected][indexPath.row]
            tableView.reloadRows(at: indexPath, with: UITableViewRowAnimation.none)

       }
}

如有任何其他说明,请随时询问

【讨论】:

  • if parentCategoryVC.checkedValues[parentCategoryVC.indexSelected].count == 0 { ///// 此处抛出错误(索引超出范围)
  • 让我检查并修复它。
  • 立即尝试。很抱歉这个错误,但我现在无法测试它
【解决方案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()
    }
}

【讨论】:

  • 复选标记在我选择单元格时正确显示,但当我返回并再次进入同一类别时不会出现
  • 哦,非常感谢!您的回答帮助我保存了选定单元格的状态!
猜你喜欢
  • 2016-06-19
  • 1970-01-01
  • 2021-09-05
  • 2012-08-25
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 2013-06-08
相关资源
最近更新 更多