当用户点击“接受”按钮时,为什么表格视图中的单元格会重复?

时间:2017-08-28 19:59:03

标签: ios uitableview firebase swift3 firebase-realtime-database

我正在开发一个应用程序,用户可以向另一个用户发送好友请求,接收请求的用户可以接受或删除它。它按预期工作。但是,当用户单击“接受”按钮时,tableView(显示已接收的请求)的单元格会重复。当我重新加载表视图或重新启动应用程序时,它会显示所需的结果,即只显示待处理的请求。此外,数据按预期存储在firebase数据库中。唯一关心的是最初复制细胞背后的自发性和错误。此外,我如何自定义单元格,以便每当用户单击“接受”或“删除”按钮时,只有该单元格变暗,然后从表格中删除?

编辑:

我尝试过使用viewDidLoad代替viewDidAppear!

接收请求函数的代码:

  @IBAction func acceptButton(_ sender: Any) {

    // add the user to current user's friend list

    ref = Database.database().reference()

    let currentUserRef = ref.child("users").child(currentUserID!)

    currentUserRef.observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.hasChild("friends"){

            self.ref.child("users").child(self.currentUserID!).child("friends").updateChildValues([self.hiddenIDLabel.text!: "true"])

        } else{

            self.ref.child("users").child(self.currentUserID!).child("friends").setValue([self.hiddenIDLabel.text!: "true"])
        }

        print("user successfully added as a friend to current user's list")

    })

    // remove this request from user's received requests

    let currentUserDeleteRef = ref.child("users").child(currentUserID!)

    currentUserDeleteRef.observeSingleEvent(of: .value, with: { (snapshot) in

        self.ref.child("users").child(self.currentUserID!).child("requests").child(self.hiddenIDLabel.text!).removeValue()

    })

    // add the current user to the user's friend list

    let senderRef = ref.child("users").child(hiddenIDLabel.text!)

    senderRef.observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.hasChild("friends"){

            self.ref.child("users").child(self.hiddenIDLabel.text!).child("friends").updateChildValues([self.currentUserID!: "true"])

        } else{

            self.ref.child("users").child(self.hiddenIDLabel.text!).child("friends").setValue([self.currentUserID!: "true"])
        }

        print("user successfully added as a friend to sender's list")

    })

    // remove this request from sender's sent requests

    let senderDeleteRef = ref.child("users").child(hiddenIDLabel.text!)

    senderDeleteRef.observeSingleEvent(of: .value, with: { (snapshot) in

        self.ref.child("users").child(self.hiddenIDLabel.text!).child("requests").child(self.currentUserID!).removeValue()

    })

}

最后,细胞代码:

<?php 
$data = array(
    "johnny bairstow alex hales jonh marush",
    "Marcelo nash tim pane alexender",
    "chaudary Mian Muhammad Aqeel shaib"
);

$output = [];
foreach($data as $names) {
    $first    = strtok($names, ' ');
    $second   = strtok(' ');
    $the_rest = strtok(null);
    $output[] = $first . ' ' . $second;
    $output[] = $the_rest;
}
var_export($output);

My firebase database

0 个答案:

没有答案