前一个视图控制器中的“更新”按钮

时间:2018-10-24 12:12:46

标签: swift uitableview uinavigationcontroller uibutton

我正在使用NavigationController显示某个用户的关注者列表。在此列表(UITableView)中,有个人资料图片,用户名,最右边是“跟随”或“取消关注”按钮。

我设法实现了一个功能,该功能可以检查用户是否已经关注,因此可以清楚地显示关注或不关注。在一个用户开始关注另一个用户之后,我也能够更新按钮,并且取消关注也是如此。

但是,可以单击用户名或个人资料图片,然后将NavigationController推到被单击的用户的个人资料上。

navViewController?.pushViewController(userVC, animated: true)

在配置文件页面上是相同的“跟随”或“取消关注”按钮,具有相同的逻辑(但略有改动,因为我不需要在配置文件页面上使用indexPaths等)。一切正常,除非我从列表中进入某人的个人资料页面,然后开始关注或取消关注,然后使用NavigationController上的“后退”按钮返回列表。它仍然显示之前的文本并应用错误的逻辑(如果我取消关注个人资料,请在列表中返回并取消关注(这不可能,但按钮尚未更新),我创建了一个双重取消关注,排序的话。

我正在寻找一种更新上一个视图(UITableView)上的按钮的方法。有任何想法吗?我不能使用.observe而不是.observeSingleEvent,因为这将创建一个跟随和取消跟随的循环,因为获取列表的代码如下:

func getUsers() {

    var Ref: DatabaseReference!
    Ref = Database.database().reference()

    Ref.child("users").queryOrdered(byChild: "userLastName").observeSingleEvent(of: .value) { (snapshotUsers) in

        for child in snapshotUsers.children {

            let snap = child as! DataSnapshot
            let dict = snap.value as! [String: Any]

            if(snap.key != Auth.auth().currentUser?.uid) {
                Ref.child("users").child(snap.key).child("userFollowers").child((Auth.auth().currentUser?.uid)!).observeSingleEvent(of: .value) { (checkFollow) in

                    if checkFollow.exists() {

                        self.myArray.append(usersToFollow(userID: snap.key, userName: (dict["userFirstName"] as! String) + " " + (dict["userLastName"] as! String), userFirstName: (dict["userFirstName"] as! String), userLastName: (dict["userLastName"] as! String), userProfilePicURL: (dict["userProfilePicURL"] as! String), isFollowing: true))

                    } else {

                        self.myArray.append(usersToFollow(userID: snap.key, userName: (dict["userFirstName"] as! String) + " " + (dict["userLastName"] as! String), userFirstName: (dict["userFirstName"] as! String), userLastName: (dict["userLastName"] as! String), userProfilePicURL: (dict["userProfilePicURL"] as! String), isFollowing: false))

                    }

                    self.myTableView.reloadData()

                }

            }

        }

    }

}

在我的UITableView中如何解决此问题的任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以简单地使用NSNotification来调用tableView.reloadData(): 将此添加到您的tableView Controller(TVC)ViewDidLoad:

NotificationCenter.default.addObserver(self, selector: #selector(reload), name: NSNotification.Name(rawValue: "load"), object: nil)

此功能可用于您的TVC

@objc func reload()
{
      //setup ur new data
      self.tableView.reloadData()

}

然后只需将其添加到用户pf VC中的后退按钮IBAction中

  NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)
相关问题