ios - 检索 UserDefaults 后刷新 viewdidload 上的 collectionView

标签 ios swift collections view

我有一个 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#>
}

在这个函数中,你应该:

  1. cell 转换到您的 OLLCell 中(如果您想彻底,请安全地进行)
  2. 查看您的数据并查看是否应选择该单元格 OLLData.OLLCasesList[indexPath.item]._isSelected
  3. 根据您的 ._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/

相关文章:

ios - 验证阿拉伯语的名字和姓氏

macos - 如何使用 Swift 获取 TextField 值来制作 Mac App

arrays - 将我自己的类保存为 Parse 中的数组

ios - 检查字典中的对象是否为 Int (Swift)

javascript - 根据react中对象的索引渲染不同的html

ios - OpenCV 错误 : Assertion failed on iOS

ios - 为什么 iOS 5.0 不喜欢纯窗口应用程序?为什么它要求使用 View Controller ?

Java 集合排序 BigDecimal

ios - UISlider缩略图位置偏移

java - 关于 mongodb capped collection 的困惑