【发布时间】: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收集数据的索引值。
-
收集或索引要删除的值