【发布时间】:2020-01-01 09:50:55
【问题描述】:
如何将UICollectionView 的选定状态保存在UITableViewCell 中?
有关更多详细信息,我有一个 UITableView 有 5 个部分,每个部分只有一个单元格,我将另一个 UICollectionView 放入表格视图的单元格中,每当我选择一个集合视图单元格的项目时,它都会突出显示有红色背景。
现在我想保存集合视图的选择状态,即使我关闭视图控制器然后再次打开它,它必须显示正确的选定项目,我想我将使用 UserDefaults 进行保存。但我注意到,当我在另一个部分中选择一个集合视图项时,它总是保存与表视图的第一部分相同的索引。
这是我将选定的索引路径保存到数组的代码,你能告诉我我的错误在哪里吗:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let strData = itemFilter[indexPath.section].value[indexPath.item]
let cell = collectionView.cellForItem(at: indexPath) as? SDFilterCollectionCell
cell?.filterSelectionComponent?.bind(title: strData.option_name!, style: .select)
cell?.backgroundColor = .red
cell?.layer.borderColor = UIColor.white.cgColor
arrSelectedIndex.append(indexPath)
}
当取消选择时:
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let strData = itemFilter[indexPath.section].value[indexPath.item]
let cell = collectionView.cellForItem(at: indexPath) as? SDFilterCollectionCell
cell?.filterSelectionComponent?.bind(title: strData.option_name!, style: .unselect)
cell?.backgroundColor = .white
cell?.layer.borderColor = UIColor.black.cgColor
if arrSelectedIndex.count > 0 {
arrSelectedIndex = arrSelectedIndex.filter({$0 != indexPath})
}else {
arrSelectedIndex.removeAll()
}
}
【问题讨论】:
标签: ios swift uitableview uicollectionview