簡體   English   中英

檢索UserDefaults后刷新viewdidload上的collectionView

[英]Refresh collectionView on viewdidload after retrieving UserDefaults

我有一個收藏夾視圖,您可以選擇其中的項目並通過更改背景顏色來打開和關閉它們。 多虧了我為所有單元格制作的箭頭中的布爾值,這些單元格得以打開/關閉。 我已經保存了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是指示單元格是否“切換”的布爾值。

任何想法將不勝感激。

首先,取消注釋該行將產生無限循環。 之所以會發生cellForRowAt是因為正在重新加載集合視圖,因此在刷新集合視圖時調用刷新是不好的。

因此,您的問題是您不知道如何在集合視圖中顯示選定的單元格,對嗎?

這是一個在集合視圖將要顯示單元之前立即觸發的函數:

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

在此函數內,您應該:

  1. cell投射到您的OLLCell (如果您想透徹的話,可以安全地進行)
  2. 查看您的數據,看看是否應該選擇該單元格OLLData.OLLCasesList[indexPath.item]._isSelected
  3. 要求._isSelected單元格根據您的._isSelected布爾值更改其顏色/ UI /外觀

步驟3有一個非常重要的警告。 ._isSelected為false ._isSelected為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 
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM