如果我使用一个SnapshotListener,如何在TableView中对行和节进行动画删除?

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

如果我使用的是SnapshotListener,每次添加和删除数据时都会被调用,那么我怎样才能让删除行和节的动作产生动画效果?现在,当我滑动删除时,没有动画,行和节只是突然消失,这是有道理的,因为我没有实现动画,也没有手动删除行和节。这是我的loadData...

feedback = self.rootWorkoutsCollection.addSnapshotListener({ (snapshot, err) in
            let group = DispatchGroup()
            group.enter()

            self.workoutsCollection.daysCollection.removeAll()

            if let err = err
            {
                print("Error getting documents: \(err.localizedDescription)")
            }
            else {
                guard let workoutDocuments = snapshot?.documents else { return }

                for document in workoutDocuments {
                    var foundIt = false

                    let workoutData = document.data()
                    let day = workoutData["Day"] as! String
                    let workout = workoutData["Workout"] as! String

                    if self.workoutsCollection.daysCollection.isEmpty {

                        let newWorkout = Workout(Day: day, Workout: workout, Ref: document.reference)
                        let newDay = Day(Day: day, Workout: newWorkout, Ref: newWorkout.key)
                        self.workoutsCollection.daysCollection.append(newDay)
                        continue
                    }

                    if !foundIt{
                        for dayObject in self.workoutsCollection.daysCollection{
                            for dow in dayObject.workout{
                                if dow.day == day{
                                    let newWorkout = Workout(Day: day, Workout: workout, Ref: document.reference)
                                    dayObject.workout.append(newWorkout)
                                    foundIt = true
                                    break
                                }
                            }
                        }
                    }

                    if foundIt == false{
                        let newWorkout = Workout(Day: day, Workout: workout, Ref: document.reference)
                        let newDay = Day(Day: day, Workout: newWorkout, Ref: newWorkout.key)
                        self.workoutsCollection.daysCollection.append(newDay)
                    }

                }

            }
            group.leave()
            group.notify(queue: .main){
                self.tableView.reloadData()
            }
            }
        )

这里是我用来从后台和本地删除数据的函数... ...

func removeWorkout(Dow: Day, Workout: Workout ){

        let db : Firestore!
        db = Firestore.firestore()
        db.collection("Users").document("\(Auth.auth().currentUser!.uid)").collection("Workouts").document(Dow.key.documentID).delete()

        if let dayIndex = daysCollection.firstIndex(of: Dow), let workoutIndex = daysCollection[dayIndex].workout.firstIndex(of: Workout) {
            daysCollection[dayIndex].workout.remove(at: workoutIndex)
        }

    }
ios firebase google-cloud-firestore tableview
1个回答
1
投票

诀窍是不要循环 snapshot?.documents而不是使用 snapshot.documentChanges. 该 documentChanges 集合包含了上一个查询快照和这个查询快照之间每个文档是如何变化的信息。

从Firebase的文档中可以看到 查看快照之间的变化:

db.collection("cities").whereField("state", isEqualTo: "CA")
    .addSnapshotListener { querySnapshot, error in
        guard let snapshot = querySnapshot else {
            print("Error fetching snapshots: \(error!)")
            return
        }
        snapshot.documentChanges.forEach { diff in
            if (diff.type == .added) {
                print("New city: \(diff.document.data())")
            }
            if (diff.type == .modified) {
                print("Modified city: \(diff.document.data())")
            }
            if (diff.type == .removed) {
                print("Removed city: \(diff.document.data())")
            }
        }
    }

然后您可以使用这些额外的信息,通过调用 reloadRows, insertRowsdeleteRows 与上述文件。

© www.soinside.com 2019 - 2024. All rights reserved.