检索UserDefaults后刷新viewdidload上的collectionView

时间:2019-04-22 00:08:17

标签: ios swift collections view

我有一个收藏夹视图,您可以选择其中的项目并通过更改背景颜色来打开和关闭它们。多亏了我为所有单元格制作的箭头中的布尔值,这些单元格得以打开/关闭。我已经保存了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是表示单元格是否“切换”的布尔值。

任何想法都将不胜感激。

1 个答案:

答案 0 :(得分: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步有一个 VERY 重要警告。当._isSelected为false且为true时,您应该更改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 
    }
}