【发布时间】:2018-01-08 03:38:09
【问题描述】:
我在将用户数据检索到 TableView 时遇到问题,我的代码有效,并且我在单元格中看到用户的姓名,但 问题是当其中一个用户对其个人资料进行任何更改时,他的单元格将重复。
firebase 结构:
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 中的更新效果很好,但是如果其中一个键更改了他的名字,它将显示他的旧\新单元格,因此他的名字重复了。
【问题讨论】:
-
为什么没有回答 ;o
标签: ios uitableview firebase swift3 firebase-realtime-database