2017-08-01 79 views
2

我有麻煩,檢索用戶數據到TableView,我的代碼工作,我看到用戶在單元格中他們的名字,但問題是當其中一個用戶已經對他們的個人資料進行了任何更改他的單元格將重複Firebase檢索用戶到tableView

火力結構:

enter image description here

viewDidLoad中和細胞行:

override func viewDidLoad() { 
    super.viewDidLoad() 

    ref = FIRDatabase.database().reference() 

    USER_REF.keepSynced(true) 
    friends_REF.keepSynced(true) 


    showUsersObserver {} 


} 


func 

tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return self.friendList.count 

    } 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let Cell:TableViewCellx = tableView.dequeueReusableCell(withIdentifier: "mainCell") as! TableViewCellx 



     Cell.name.text = self.friendList[indexPath.row].displayname 
return(Cell) 
} 

這讓用戶在朋友的孩子結構鍵。

func showUsersObserver(_ update: @escaping() -> Void) { 
    CURRENT_USER_FRIENDS_REF.observe(.value, with: { (snapshot) in 
     self.friendList.removeAll() 


     let keys = snapshot.children 
     while let rest = keys.nextObject() as? FIRDataSnapshot { 



      self.getUser(rest.key, completion: { (user) in 
       self.friendList.append(user) 

       print(self.friendList.count) // This increase each time when user changing his name! or any changes hits his profile. 

       DispatchQueue.main.async { 
        self.tableview.reloadData() 
       } 

       update() 
      }) 

     } 



     // If there are no children, run completion here instead 
     if snapshot.childrenCount == 0 { 
      update() 
     } 
    }) 
} 

這讓他們的個人資料數據:

func getUser(_ userID: String, completion: @escaping (User) -> Void) { 
    USER_REF.child(userID).observe(.value, with: { (snapshot) in 

     guard let dictionary = snapshot.value as? [String: Any] else { 
      return 
     } 



     let id = dictionary["uid"] as? String 
     let email = dictionary["email"] as? String 
     let DisplayName = dictionary["displayname"] as? String 




     completion(User(userEmail: email!, userID: id!, userDisplayName: DisplayName!)) 



    }) 
} 

而且好友列表:

class User { 

var uid: String! 
var displayname: String! 
var email: String! 


init(userEmail: String, userID: String, userDisplayName: String) { 

    self.email = userEmail 
    self.uid = userID 
    self.displayname = userDisplayName 

    } 
} 

當我添加\在朋友的孩子取下鑰匙,我看到我的tableView作品更新好,但如果其中一個鍵已經改變了他的名字,它會顯示他的舊\新單元,所以他的名字重複。

+0

爲什麼沒有aswers;○ – Sam

回答

0

保留與用戶名關聯的tableView中每行的唯一標識符。

當用戶更新後,從用戶列表中獲取數據,然後更新該特定項目。

您可以使用用戶密鑰作爲唯一標識符

感謝

+0

感謝您的答覆,但我沒有得到它,你能給我們解釋更多,並添加一些代碼? – Sam

相關問題