【问题标题】:Refresh collectionView on viewdidload after retrieving UserDefaults检索 UserDefaults 后在 viewdidload 上刷新 collectionView
【发布时间】:2019-04-22 00:08:17
【问题描述】:

我有一个集合视图,您可以选择其中的项目并通过更改背景颜色来打开和关闭它们。由于我为所有单元格制作的箭头中的布尔值,单元格被打开/关闭。我已经保存了布尔值,但是当我尝试将它们写回数组并使用collectionView.reloadData()时,应用程序崩溃了。我的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是表示单元格是否“切换”的布尔值。

任何想法将不胜感激。

【问题讨论】:

  • 崩溃日志说什么?
  • Thread 1: EXC_BAD_ACCESS (code=2, address=0x16d79f410) 以前从未见过这种错误。
  • 永远不要从cellForRowAt打电话给reloadData
  • 嗯好吧 - 但你知道我如何刷新选定的项目吗?
  • 当您从 cellForRowAt 调用 reloadData 时,您正在创建一个无限循环。 cellForRowAt 发生在集合视图重新加载时,所以如果你在里面调用它,你可以看到调用永远不会结束。

标签: ios swift collections view


【解决方案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 为假且为真时,您应该更改用户界面。因为集合视图重用单元格,旧的 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 
    }
}

【讨论】:

  • 这会违背我对 didSelectItemAt 所做的吗?还有,你说的演员是什么意思?我对 swift 很陌生,不熟悉术语...
  • 你在你的cellForItemAt函数中写了这行let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "OLLCell", for: indexPath) as! OLLCell。发生的事情是您要求您的集合视图重用其具有“OLLCell”ID 的单元格之一。只需向您的集合视图询问dequeueReusableCell 将返回一个 UICollectionViewCell。当您执行 as! OLLCell 时,您正在强制 UICollectionViewCell 成为 OLLCell,或者更确切地说,您正在强制转换它。
  • 在我的回答中,我正在做cell as? OLLCell。我输入的是as?,而不是你的as!。这是一个安全的演员表,当你有很多不同的类型的细胞时很有用。它基本上是在问“嘿,UICollectionView 先生,这里的 UICollectionViewCell 是 OLLCell 吗?”如果它说“是”,那么您可以访问 OLLCell 的 imageView。
  • 不,我认为这与您的 didSelectItemAt 功能不冲突。您不会更改其中的任何 UI,而只是更改支持数据状态。在我的 willDisplayCell 函数中,我只是在读取您的支持数据状态并根据它所说的切换 UI 颜色。
  • 没问题,如果我有帮助,请点赞我的回答。如果这不能解决您的问题,请告诉我。
猜你喜欢
  • 2021-03-09
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
相关资源
最近更新 更多