检索UserDefaults后,在viewdidload上刷新collectionView

问题描述 投票:0回答:1

我有一个集合视图,您可以选择其中的项目,并通过更改背景颜色来打开和关闭它们。由于我在为所有细胞制作的箭头中有一个布尔值,因此打开/关闭单元格。我保存了bool值,但是当我尝试将它们写回数组并使用collectionView.reloadData()the app崩溃时。我的collectionViewcode是:

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是表示单元格是否“切换”的布尔值。

任何想法将不胜感激。

ios swift collections view
1个回答
1
投票

首先,取消注释该行将产生无限循环。 cellForRowAt发生是因为集合视图正在重新加载,所以在集合视图刷新时调用刷新是不好的。

所以你的问题是你不知道如何在集合视图中显示选定的单元格,对吧?

这是一个在集合视图即将显示单元格之前触发的函数:

func collectionView(_ collectionView: UICollectionView, 
                    willDisplay cell: UICollectionViewCell, 
                    forItemAt indexPath: IndexPath)
{
    <#code#>
}

在此功能中,您应该:

  1. cell投入你的OLLCell(如果你想要彻底安全的话)
  2. 查看您的数据,看看是否应该选择单元格OLLData.OLLCasesList[indexPath.item]._isSelected
  3. 根据你的._isSelected布尔值,让你的铸造单元改变它的颜色/ UI /外观

第3步有一个非常重要的警告。当._isSelected为false时,您应该更改UI以及何时为真。因为集合视图重用单元格,旧的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 
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.