菜鸟教程小白 发表于 2022-12-11 18:24:59

ios - Firebase 将用户检索到 tableView


                                            <p><p>我在将用户数据检索到 TableView 时遇到问题,我的代码有效,并且我在单元格中看到用户的姓名,但 <strong>问题是当其中一个用户对其个人资料进行任何更改时,他的单元格将重复</strong>。</p>

<p>firebase 结构:</p>

<p> <a href="/image/Hxm1Q.png" rel="noreferrer noopener nofollow"><img src="/image/Hxm1Q.png" alt="enter image description here"/></a> </p>

<p>ViewDidLoad 和单元格行:</p>

<pre><code>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) -&gt; Int {

      return self.friendList.count

    }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

      let Cell:TableViewCellx = tableView.dequeueReusableCell(withIdentifier: &#34;mainCell&#34;) as! TableViewCellx



      Cell.name.text = self.friendList.displayname
return(Cell)
}
</code></pre>

<p>这是在 friend 子结构中获取用户的 key 。</p>

<pre><code> func showUsersObserver(_ update: @escaping () -&gt; 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()
      }
    })
}
</code></pre>

<p>这是获取他们的个人资料数据:</p>

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

      guard let dictionary = snapshot.value as? else {
            return
      }



      let id = dictionary[&#34;uid&#34;] as? String
      let email = dictionary[&#34;email&#34;] as? String
      let DisplayName = dictionary[&#34;displayname&#34;] as? String




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



    })
}
</code></pre>

<p>还有 friend 列表:</p>

<pre><code>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

    }
}
</code></pre>

<p>当我在 friendchild 中添加\删除键时,我看到我的 tableView 中的更新效果很好,但是如果其中一个键更改了他的名字,它将显示他的旧\新单元格,因此他的名字重复了。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>保持 <code>tableView</code> 中每一行的唯一标识符与用户名相关联。</p>

<p>当用户更新时,从用户列表中获取数据,然后更新该特定项目。</p>

<p>您可以使用用户 key 作为唯一标识符</p>

<p>谢谢</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Firebase 将用户检索到 tableView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45434957/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45434957/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Firebase 将用户检索到 tableView