向不同部分的单元格添加多个复选标记,并将其状态保存在Swift iOS中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了向不同部分的单元格添加多个复选标记,并将其状态保存在Swift iOS中相关的知识,希望对你有一定的参考价值。

在我的应用中,我有5个部分,其中包含一些数据。我要选中的标记是一次在第1节说2个单元格,然后在第2节说3个单元格,依此类推。在工作的不同部分以及该单元格中选中的任何单元格中的多项选择,我都有一个标签标题,我将其添加并存储在userdefaults中,因为该标题需要在上一个屏幕中显示。现在,当我从第1部分中选择了2个单元格,然后从第2部分中选择了2个单元格并返回时,则上一个屏幕中仅显示最新选择的单元格标题,这是错误的,它应该显示4个标题,并用逗号分隔,这是我在先前屏幕中编写的代码。不知道该怎么办

我的代码:-

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "allAssetCell", for: indexPath) as! AllAssetCell

        var mainArr = [SymName]()

        if indexPath.section == digitalIndex
        {
            mainArr = isSearch ? filtereddigitalArr : digitalArr
        }
        else if indexPath.section == forexIndex
        {
            mainArr = isSearch ? filteredforexArr : forexArr
        }
        else if indexPath.section == stocksIndex
        {
            mainArr = isSearch ? filteredstocksArr : stocksArr
        }
        else if indexPath.section == cryptoIndex
        {
            mainArr = isSearch ? filteredcryptoArr : cryptoArr
        }
        else if indexPath.section == commoditiesIndex
        {
            mainArr = isSearch ? filteredcommoditiesArr : commoditiesArr
        }
        else if indexPath.section == etfIndex
        {
            mainArr = isSearch ? filteredetfArr : etfArr
        }
        else
        {
            mainArr = isSearch ? (filtereddigitalArr + filteredforexArr + filteredstocksArr + filteredcryptoArr + filteredcommoditiesArr + filteredetfArr) : (digitalArr + forexArr + stocksArr + cryptoArr + commoditiesArr + etfArr)
        }

        cell.lblSymbolsName.text = mainArr[indexPath.row].name
        cell.accessoryType = mainArr[indexPath.row].isSelected ? .checkmark : .none
        return cell
    }


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        tableView.deselectRow(at: indexPath, animated: true)

        var mainArr = [SymName]()
        var filteredMainArr = [SymName]()

        if indexPath.section == digitalIndex
        {
            if isSearch
            {
                filtereddigitalArr[indexPath.row].isSelected.toggle()
                let selectedNames = filtereddigitalArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                digitalArr[indexPath.row].isSelected.toggle()
                let selectedNames = digitalArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }
        else if indexPath.section == forexIndex
        {
            if isSearch
            {
                filteredforexArr[indexPath.row].isSelected.toggle()
                let selectedNames = filteredforexArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                forexArr[indexPath.row].isSelected.toggle()
                let selectedNames = forexArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }
        else if indexPath.section == stocksIndex
        {
            if isSearch
            {
                filteredstocksArr[indexPath.row].isSelected.toggle()
                let selectedNames = filteredstocksArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                stocksArr[indexPath.row].isSelected.toggle()
                let selectedNames = stocksArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }
        else if indexPath.section == cryptoIndex
        {
            if isSearch
            {
                filteredcryptoArr[indexPath.row].isSelected.toggle()
                let selectedNames = filteredcryptoArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                cryptoArr[indexPath.row].isSelected.toggle()
                let selectedNames = cryptoArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }
        else if indexPath.section == commoditiesIndex
        {
            if isSearch
            {
                filteredcommoditiesArr[indexPath.row].isSelected.toggle()
                let selectedNames = filteredcommoditiesArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                commoditiesArr[indexPath.row].isSelected.toggle()
                let selectedNames = commoditiesArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }
        else if indexPath.section == etfIndex
        {
            if isSearch
            {
                filteredetfArr[indexPath.row].isSelected.toggle()
                let selectedNames = filteredetfArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                etfArr[indexPath.row].isSelected.toggle()
                let selectedNames = etfArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }
        else
        {
            mainArr = digitalArr + forexArr + stocksArr + cryptoArr + commoditiesArr + etfArr
            filteredMainArr = filtereddigitalArr + filteredforexArr + filteredstocksArr + filteredcryptoArr + filteredcommoditiesArr + filteredetfArr

            if isSearch
            {
                filteredMainArr[indexPath.row].isSelected.toggle()
                let selectedNames = filteredMainArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
            else
            {
                mainArr[indexPath.row].isSelected.toggle()
                let selectedNames = mainArr.filter{$0.isSelected}.map{$0.name}
                UserDefaults.standard.set(selectedNames, forKey: "selectedNames")

                if selectedNames.count > 0
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: selectedNames)
                }
                else
                {
                    ApiUtillity.sharedInstance.saveAssetTypeTextUserDefaults(arr: ["All Assets"])
                }
            }
        }

        tableView.reloadRows(at: [indexPath], with: .none)
    }
答案

您需要在didSelect方法之外声明这两个变量

var mainArr = [SymName]()
var filteredMainArr = [SymName]()

因为当您选择一个单元格时,它将创建新的数组,而先前的单元格将被丢弃。

以上是关于向不同部分的单元格添加多个复选标记,并将其状态保存在Swift iOS中的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 3 中保存 UITableViewCell 附件复选标记的状态

如何在加载了 JSON API 数据的 tableview 单元格中保存复选标记属性?

如何从其他 ViewController 向 TableViewController 添加复选标记

UICollectionView didSelectItemAt() 并根据状态添加/删除视图

在滚动 UITableViewCell 添加按钮图像问题

清除搜索结果后,Swift Tableview 搜索结果单元格选择复选标记未保存