我有一个 Collection View ,您可以选择其中的项目并通过更改背景颜色来打开和关闭它们。由于我在为所有单元格制作的箭头中有一个 bool 值,单元格可以打开/关闭。我已经保存了 bool 值,但是当我尝试将它们写回数组并使用 collectionView.reloadData()
时,应用程序崩溃了。我的 collectionView
代码是:
extension OLLViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { //set the amount of items in the CollectionView to the amount of items in the OLLData dictionary
return OLLData.OLLCasesList.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //set each cell to a different mamber of the dict.
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OLLCell", for: indexPath) as! OLLCell
cell.imageView.backgroundColor = OLLData.OLLCasesList[indexPath.item]._isSelected ? UIColor.orange : UIColor.clear //change colour if selected
let image = OLLData.OLLCasesList[indexPath.item]._imageName
cell.label.text = image
cell.imageView.image = UIImage(named: image)
let savedIsSelected = defaults.bool(forKey: Key.isSelected)
OLLData.OLLCasesList[indexPath.item]._isSelected = savedIsSelected
//collectionView.reloadData() //when uncommented it crashes the app
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { //detect if case selected and reload CollectionView
let caseName = OLLData.OLLCasesList[indexPath.item]._imageName
print(caseName, OLLData.OLLCasesList[indexPath.item]._isSelected)
OLLData.OLLCasesList[indexPath.item]._isSelected = !OLLData.OLLCasesList[indexPath.item]._isSelected
defaults.set(OLLData.OLLCasesList[indexPath.item]._isSelected, forKey: Key.isSelected)
collectionView.reloadItems(at:[indexPath])
collectionView.reloadData()
if OLLData.OLLCasesList[indexPath.item]._isSelected == true { //if the item is selected, add to selectedCases array
selectedCases.append(OLLData.OLLCasesList[indexPath.item]._id)
selectedCaseNames.append(OLLData.OLLCasesList[indexPath.item]._imageName)
print(selectedCases, selectedCaseNames) //debugging
numberOfSelectedCases.text = String(selectedCases.count)
}
else if OLLData.OLLCasesList[indexPath.item]._isSelected == false { //remove from selectedCases array
selectedCases.removeAll(where: { $0 == OLLData.OLLCasesList[indexPath.item]._id })
selectedCaseNames.removeAll(where: { $0 == OLLData.OLLCasesList[indexPath.item]._imageName })
print(selectedCases, selectedCaseNames) //debugging
numberOfSelectedCases.text = String(selectedCases.count)
}
}
._isSelected
是表示单元格是否“切换”的 bool 值。
任何想法将不胜感激。
最佳答案
首先,取消注释该行将产生无限循环。 cellForRowAt
发生是因为 Collection View 正在重新加载,因此在刷新 Collection View 时调用刷新是不好的。
所以您的问题是您不知道如何在 Collection View 中显示选定的单元格,对吧?
这是一个在 Collection View 即将显示单元格之前触发的函数:
func collectionView(_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath)
{
<#code#>
}
在这个函数中,你应该:
- 将
cell
转换到您的OLLCell
中(如果您想彻底,请安全地进行) - 查看您的数据并查看是否应选择该单元格
OLLData.OLLCasesList[indexPath.item]._isSelected
- 根据您的
._isSelected
bool 值让您的类型转换单元更改其颜色/UI/外观
第 3 步有一个非常重要的警告。当 ._isSelected
为 false 和为 true 时,您应该更改 UI。因为 Collection View 重用了单元格,所以旧的 UI 状态会随机出现。所以每次都设置它是确保您想要的行为的好方法。
这是一个例子:
func collectionView(_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath)
{
//Cast the vanilla cell into your custom cell so you have access
//to OLLCell's specific functions and properties.
//Also make sure the indexPath falls in the indices of your data
if let myCastedCell = cell as? OLLCell,
0 ..< OLLData.OLLCasesList.count ~= indexPath.item
{
myCastedCell.imageView.backgroundColor = OLLData
.OLLCasesList[indexPath.item]._isSelected
? UIColor.orange
: UIColor.clear
}
}
关于ios - 检索 UserDefaults 后刷新 viewdidload 上的 collectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787820/