取消关注用户swift后,表视图不重新载体



当我关注用户帖子时,我正在使用firebase在 UITableView中很好地显示出他的帖子。但是当取消关注他时,他的职位仍在UITableView中。 UITableView未关注后不重新加载数据。

查看控制器:

import UIKit
import SVProgressHUD
import SDWebImage
class HomeVC: UIViewController  {
    @IBOutlet weak var tableViewOutLet: UITableView!
    @IBOutlet weak var activityIndicatorView: UIActivityIndicatorView!
    var postContentArray = [Post]() // contine all Posts .
    var userContentArray = [UserModel]()
    override func viewDidLoad() {
        super.viewDidLoad()
        tableViewOutLet.dataSource = self
        tableViewOutLet.estimatedRowHeight = 521
        tableViewOutLet.rowHeight = UITableView.automaticDimension
        loadPosts()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tabBarController?.tabBar.isHidden = false
    }

    @objc func loadPosts () {
      //  activityIndicatorView.startAnimating()
        API.Feed.ObserveFeedWithMyPostAndFollowerPost(withid: API.User.Curren_User!.uid) { (postr) in
            self.fetchUserInfo(PostUserID: postr.userPostId!, Completed: {
            self.postContentArray.append(postr)
          //  self.activityIndicatorView.stopAnimating()
            self.tableViewOutLet.reloadData()
                            })
        }
        API.Feed.ObserveFeedReomved(withid: API.User.Curren_User!.uid) { (key) in
            // print(key)
            self.postContentArray = self.postContentArray.filter {$0.userPostId != key }
            self.tableViewOutLet.reloadData()
        }
    }


    @objc func fetchUserInfo (PostUserID : String , Completed : @escaping ()-> Void) {
        API.User.observeUserInformation(CommentUserID: PostUserID) { (User) in
            self.userContentArray.append(User)
            Completed()
        }
    }
    @IBAction func logOutButton(_ sender: Any) {
        AuthServices.logout(OnSuccess: {
        SVProgressHUD.showSuccess(withStatus: "تم تسجيل الخروج")
            let storyBoard = UIStoryboard(name: "Start", bundle: nil)
            let signInvc = storyBoard.instantiateViewController(withIdentifier: "SignInVC")
            self.present(signInvc, animated: true, completion: nil)
        }) { (error) in
            SVProgressHUD.showError(withStatus: error)
        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "commentIconSequeToCommentPage" {
            let commentvc = segue.destination as! CommentsVC
            commentvc.postID = sender as? String
        }
    }
}
extension HomeVC: UITableViewDataSource  {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return postContentArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! HomeTableViewCell
        let postContent = postContentArray[indexPath.row]
        let userContent = userContentArray[indexPath.row]
        cell.postContent = postContent
        cell.userContentInfo = userContent
        cell.homeView = self
        return cell
    }
}

观察功能:

import Foundation
import  FirebaseDatabase
class FeedAPI {
    var REF_FEED = Database.database().reference().child("Feed")
    func ObserveFeedWithMyPostAndFollowerPost (withid id : String , complation : @escaping (Post)->Void) {
        REF_FEED.child(id).observe(DataEventType.childAdded) { (snapshto : DataSnapshot) in
            let key = snapshto.key
            API.Post.observePostFuntion(withID: key, Complation: { (post) in
             complation(post)
            })
        }
    }
    func ObserveFeedReomved (withid id : String , complation : @escaping (String)->Void) {
        REF_FEED.child(id).observe(DataEventType.childRemoved) { (snapshot : DataSnapshot) in
            let key = snapshot.key
            complation(key)
        }
    }
}

脱离函数到底在哪里?

您还应该设置tableview委托

override func viewDidLoad() {
 tableViewOutLet.delegate = self
}

请显示您确切地在何处预先添加unlow函数。

我的关注和未关注func

class FollowAPI {
    var REF_FOLLOWERS = Database.database().reference().child("Follower")
    var REF_FOLLOWING = Database.database().reference().child("Following")

    func followAction (withId id : String) {
        API.Follow.REF_FOLLOWERS.child(id).child(API.User.Curren_User!.uid).setValue(true)
        API.Follow.REF_FOLLOWING.child(API.User.Curren_User!.uid).child(id).setValue(true)
        API.myPOSTS.REF_MYPOSTS.child(id).observeSingleEvent(of: DataEventType.value) { (snapshot :  DataSnapshot) in
            print(snapshot.key)
            if let dic = snapshot.value as? [String : Any]{
                for key in dic.keys {
                    API.Feed.REF_FEED.child(API.User.Curren_User!.uid).child(key).setValue(true)
                }
            }
        }
    }
    func UnFollowAction (withid id : String) {
        API.Follow.REF_FOLLOWERS.child(id).child(API.User.Curren_User!.uid).setValue(NSNull())
        API.Follow.REF_FOLLOWING.child(API.User.Curren_User!.uid).child(id).setValue(NSNull())
        API.myPOSTS.REF_MYPOSTS.child(id).observeSingleEvent(of: DataEventType.value) { (snapshot :  DataSnapshot) in
            print(snapshot.key)
            if let dic = snapshot.value as? [String : Any]{
                for key in dic.keys {
                    API.Feed.REF_FEED.child(API.User.Curren_User!.uid).child(key).removeValue()
                }
            }
        }
    }
    func isFollwoing (userid : String , Complated : @escaping (Bool)->Void) {
        REF_FOLLOWERS.child(userid).child(API.User.Curren_User!.uid).observeSingleEvent(of: .value) { (snapshot ) in
            if let _ = snapshot.value as? NSNull {
                Complated(false)
            } else {
                Complated(true)
            }
        }
    }
}

最新更新