swift xcode didSelectRowAt Indexpath只会选择一次

时间:2017-06-30 12:42:14

标签: ios swift xcode firebase firebase-realtime-database

我点击一次它就打勾了,虽然在第二次点击它时不会删除勾号,当有一个勾号时同样如此,第一次点击勾选将被删除但是一旦删除我点击再次它不会取代蜱?

当我离开vc并返回时,我只能将刻度线更改为无刻度线或无刻度线,

是否有一个动作/功能可以重新加载执行点击的功能,而不必退出vc并重新进入?

这是我的代码。

   override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let ref = FIRDatabase.database().reference()
    let uid = FIRAuth.auth()?.currentUser?.uid
    let key = ref.child("Users").childByAutoId().key
    checkIfFollowing(indexPath: indexPath)

    var isFollower = false

    ref.child("Users").child(uid!).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        if let following = snapshot.value as? [String : AnyObject] {
            for (ke, value) in following {
                if value as? String == self.users[indexPath.row].userID {
                   isFollower = true

                    ref.child("Users").child(uid!).child("following/\(ke)").removeValue()
                    ref.child("Users").child(self.users[indexPath.row].userID!).child("followers/\(ke)").removeValue()

                    self.tableView(tableView, cellForRowAt: indexPath).accessoryType = .none
                    //self.checkIfFollowing(indexPath: indexPath)
                }
            }
        }
    // if !isFoller - means isFollower is equal to false
        if !isFollower {
            let following = ["following/\(key)":self.users[indexPath.row].userID]
            let followers = ["followers/\(key)" : uid]

            ref.child("Users").child(uid!).updateChildValues(following)
            ref.child("Users").child(self.users[indexPath.row].userID!).updateChildValues(followers)


            //self.checkIfFollowing(indexPath: indexPath)

           self.tableView(tableView, cellForRowAt: indexPath).accessoryType =  .checkmark
        }
})
   ref.removeAllObservers()


}







func checkIfFollowing(indexPath: IndexPath){
    let ref = FIRDatabase.database().reference()
    let uid = FIRAuth.auth()?.currentUser?.uid

    ref.child("Users").child(uid!).child("following").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in

        if let following = snapshot.value as? [String : AnyObject] {
            for (_, value) in following {
                if value as? String == self.users[indexPath.row].userID {
                    self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark



            }
            }
        }
    })
     ref.removeAllObservers()
    }

0 个答案:

没有答案