ios - Firebase 观察者值影响所有 Collection View 单元格

标签 ios swift xcode

我在应用程序的主视图 Controller 上有一个带有 UIButton 的 Collection View ,当用户更改其状态时需要对其进行更改。

我正在使用 Firebase 观察者值,因此当值发生变化时,它会立即改变按钮颜色和图像。

问题是当我更改第二个单元格的值并向前更改时,它会将所有单元格更改为。

为了更好地理解,我添加了图片:

This is the cells before changing the value of the second one.

And this is the what happens after I changed only the second cell values on firebase

如您所见,它也会影响第一个单元格。 使用事务 block 从另一个 View Controller 更改此观察者的值。

我在谷歌上搜索了这个问题,发现了类似的问题,但没有一个答案可以解决这个问题。

这是 Collection View 的代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "group_card_cell", for: indexPath) as! HomeGroupCollectionViewCell

    cell.prepareForReuse()
    cell.groupName.text = groups[indexPath.row].name
    cell.groupSchedule.text = "\(groups[indexPath.row].day!), Around \(groups[indexPath.row].time!)"

    cell.cardMoreButtonRef.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
    cell.cardMoreButtonRef.tag = indexPath.row

        //Changing card attending color and value:
        self.ref.child("Groups").child(groups[indexPath.row].uid!).child("groupFriendsList").child(currentUser!.uid).observe(.value) { (snapshot) in
            guard let value = snapshot.value as? String else {return}

            switch value{
            case "Going":
                cell.groupAttendButtonRef.backgroundColor = UIColor.init(named: "GoingColor")
                cell.groupAttendButtonRef.setImage(UIImage(named: "icons8-checkmark_filled"), for: .normal)
            case "Not Going":
                cell.groupAttendButtonRef.backgroundColor = UIColor.init(named: "NotGoingColor")
                cell.groupAttendButtonRef.setImage(UIImage(named: "icons8-delete_sign_filled"), for: .normal)
            case "Maybe":
                cell.groupAttendButtonRef.backgroundColor = UIColor.init(named: "MaybeColor")
                cell.groupAttendButtonRef.setImage(UIImage(named: "icons8-minus_math_filled"), for: .normal)
            default:
                cell.groupAttendButtonRef.backgroundColor = UIColor.init(named: "MaybeColor")
                cell.groupAttendButtonRef.setImage(UIImage(named: "icons8-minus_math_filled"), for: .normal)
            }
        }
    return cell
}

提前致谢!

最佳答案

您是否尝试过在 Firebase 事件回调中检索特定 indexPath 的 cell
您可以使用 -
let specificCell = collectionView.cellForItem(at: IndexPath (row: row, section: section))
获取仅基于 indexPath 的单元格
或者您可以使用重用标识符获取它 -

let idpath = IndexPath(row: indexPath.row-1, section: indexPath.section)
let specificCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: idpath)


一旦您获得了单元格,您就可以相应地设置该单元格的 groupAttendButtonRef

关于ios - Firebase 观察者值影响所有 Collection View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53172249/

相关文章:

ios - 跨 iOS 设备的 float 是否具有确定性?

ios - "CL Typing Label "swift 库错误

swift - Swift 上的不可变值初始化错误?

xcode - 将一个 Xcode 项目包含在另一个项目中——链接器错误

ios - 如何在方案中将相对路径(相对于主包)传递给 iOS 应用程序作为 "argument passed on launch"?

javascript - 在 JavaScript 中检测页面是否加载到 WKWebView 中

ios - errSecInternalComponent 命令 CodeSign 失败,退出代码为 65

swift - 字符串数组减少

swift - Realm 结果按回调排序

ios - 调用 UIGraphicsBeginImageContext 时出现无效上下文 0x0 错误。调用任何 Core Graphics 'AddPath' 函数时也会出现类似的错误