【问题标题】:Deselcting a pre pupulated array swift快速取消选择预填充的数组
【发布时间】:2020-08-16 22:40:36
【问题描述】:

我有一个在底部工作表上完成的过滤器操作,然后将该值转换为字典,我在 UIView 中嵌入了一个 collectionView 来显示过滤器参数。

当一个项目被选中并按下filter 按钮时,我想使用UserDefault 保存选定的值,这样如果用户决定再次进入过滤器页面,我可以保留搜索到的项目,直到用户按下休息,然后我清除一切。

目前我的过滤器按预期工作,但我现在的问题是持久性。当我预先选择单元格并尝试取消选择该字段时,我遇到了崩溃。

这就是我的工作

fileprivate let data1 = UserDefaultsConfig.contributionTypeFilter["selectedPaymentIndex"] as? Data
    var arrSelectedIndex = [IndexPath]() // This is selected cell Index array
    var arrSelectedData = [String]()

    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.contentView.backgroundColor = UIColor.white.withAlphaComponent(0.8)
        let strData = PaymentMethodFilter.allCases[indexPath.item].rawValue.capitalized
        arrSelectedIndex.remove(at: indexPath.row)
        arrSelectedData.remove(at: indexPath.row)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let strData = PaymentMethodFilter.allCases[indexPath.item].rawValue.capitalized
        arrSelectedIndex.append(indexPath)
        arrSelectedData.append(strData)
    }


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeue(dequeueableCell: PaymentMethodFilterCollectionCell.self, forIndexPath: indexPath)
        cell.feedSubviews(with: PaymentMethodFilter.allCases[indexPath.row])
        
        guard let selectedData = data1 else {
            return cell
        }
        arrSelectedData = UserDefaultsConfig.contributionTypeFilter["payment_method_names"] as? [String] ?? []
        arrSelectedIndex = NSKeyedUnarchiver.unarchiveObject(with: selectedData) as? [IndexPath] ?? []
        arrSelectedIndex.forEach {
            collectionView.selectItem(at: $0, animated: true, scrollPosition: .right)
        }
        return cell
    }

致命错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/Array.swift, 第 1221 行 2020-08-16 15:36:26.359431+0100 Riby[40882:1056506] 致命 错误:索引超出范围:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/Array.swift, 第 1221 行

【问题讨论】:

  • 也分享崩溃日志。
  • 是索引超出范围崩溃
  • 在崩溃的行放一个断点,并检查你用来从array/s收集数据的索引值。
  • 收集或索引要删除的值

标签: swift uicollectionview


【解决方案1】:

我要保存选中的值

要记录选择的内容,请不要创建索引路径数组,因为索引路径可能会发生变化(在您的代码中,这正是发生的情况)。相反,为所选单元格表示的数据创建一个唯一标识符数组。这样,您总是可以再次找到它们。不变的是数据,这就是识别事物的方式。

基本上,您的基础数据模型应该由唯一可识别的条目组成。 (如果您使用 diffable 数据源,则将为您强制执行该规则,这只是 diffable 数据源很棒的众多原因之一。)您始终可以在集合视图中的实际数据对象和当前单元格之间双向转换.

【讨论】:

  • 保存的值是不同的字符串。如果我尝试使用索引路径删除项目,则会发生崩溃。另外,我已经将 indexpath 设置为从我的 cellForItemAt indexPath 获取的值
  • 我坚持我的回答。
  • 好的,但是在填充选定单元格时,我需要一个 indexPath 来实际选择这些单元格。
  • 是的,所以你照我说的做。您查看实际的数据对象并将它们转换为显示它们的单元格的索引路径,然后选择这些单元格。你知道它是哪个数据对象,所以你可以说,这个数据对象现在是什么索引路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多