firebaseui-ios FirebaseTableViewDataSource 与 UITableView 同步
firebaseui-ios FirebaseTableViewDataSource synchronisation with UITableView
我正在使用 FirebaseUI-IOS 和 FirebaseTableViewDataSource 绑定到我的 UITableView。对于我的实现,UITableView 应该以 'pending' 状态显示邀请以连接用户,类似于 LinkedIn。存储在 Firebase 中的邀请数据有一个状态键,可以是 'pending'、'accepted' 或 'rejected'。在用户收到 accepted/rejected 邀请后,我正在努力让 UITableView 更新并仅显示 'pending' 行。当前发生的是 UITableView 单元格得到更新(名称标签更改为其默认文本),但单元格仍显示。 UITableView 似乎是绑定到用户 'received' 节点下的所有行,而不是在状态 != 'pending'.
时响应
我在 Firebase 中的数据结构如下:
Firebase 'Invitation' data structure
我在 ViewDidLoad() 中使用以下代码来初始化 FirebaseTableViewDataSource 并填充单元格:
//Initialise a Firebase reference to the authorised user's 'received' invitations data
self.ref = Firebase(url: "\(kFBInvitationsUsersRef)/\(authData.uid)/received")
//Query the location for invitations where the status is pending only
self.ref.queryOrderedByChild("status").queryStartingAtValue(InvitationStatus.pending.rawValue).queryEndingAtValue(InvitationStatus.pending.rawValue).observeEventType(.ChildChanged , withBlock: {(snapshot) in
} )
//Assign the tableview datasource to a new FirebaseTableViewDataSource instance using the Firebase ref created above
self.dataSource = FirebaseTableViewDataSource(query: self.ref, modelClass:nil, prototypeReuseIdentifier: "invitationCell", view: self.tableView)
//Call the populateCellWithBlock method and configure the cell
self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
let cell = cell as! InvitationTableViewCell
let snap = obj as! FDataSnapshot
if let status = snap.value["status"] as? String where status == InvitationStatus.pending.rawValue {
let userRef = Firebase(url: "\(kFBUsersPublicRef)/\(snap.key)")
userRef.observeSingleEventOfType(.Value, withBlock: {snapshot in
let user = User(key: snapshot.key, displayName: snapshot.value["displayName"] as! String, givenName: snapshot.value["givenName"] as! String, familyName: snapshot.value["familyName"] as! String)
cell.user = user
cell.delegate = self
})
}
}
self.tableView.dataSource = self.dataSource
self.tableView.delegate = self
}
任何人都可以帮助诊断我在这里做错了什么吗?我的方法正确吗?
感谢您提供的任何帮助!
我已经改变了我的方法来找到解决方案。我在 Firebase 中修改了我的数据结构,以便状态成为键。然后,我将 FirebaseTableViewDataSource 绑定到 'pending' 键的引用。当用户 accepts/rejects 邀请时,它会从 'pending' 节点中删除并在 'accepted'/'rejected' 节点上创建。当添加新 'pending' 邀请或现有 'pending' 邀请为 accepted/rejected.
时,tableView 现在会按预期更新
我正在使用 FirebaseUI-IOS 和 FirebaseTableViewDataSource 绑定到我的 UITableView。对于我的实现,UITableView 应该以 'pending' 状态显示邀请以连接用户,类似于 LinkedIn。存储在 Firebase 中的邀请数据有一个状态键,可以是 'pending'、'accepted' 或 'rejected'。在用户收到 accepted/rejected 邀请后,我正在努力让 UITableView 更新并仅显示 'pending' 行。当前发生的是 UITableView 单元格得到更新(名称标签更改为其默认文本),但单元格仍显示。 UITableView 似乎是绑定到用户 'received' 节点下的所有行,而不是在状态 != 'pending'.
时响应我在 Firebase 中的数据结构如下:
Firebase 'Invitation' data structure
我在 ViewDidLoad() 中使用以下代码来初始化 FirebaseTableViewDataSource 并填充单元格:
//Initialise a Firebase reference to the authorised user's 'received' invitations data
self.ref = Firebase(url: "\(kFBInvitationsUsersRef)/\(authData.uid)/received")
//Query the location for invitations where the status is pending only
self.ref.queryOrderedByChild("status").queryStartingAtValue(InvitationStatus.pending.rawValue).queryEndingAtValue(InvitationStatus.pending.rawValue).observeEventType(.ChildChanged , withBlock: {(snapshot) in
} )
//Assign the tableview datasource to a new FirebaseTableViewDataSource instance using the Firebase ref created above
self.dataSource = FirebaseTableViewDataSource(query: self.ref, modelClass:nil, prototypeReuseIdentifier: "invitationCell", view: self.tableView)
//Call the populateCellWithBlock method and configure the cell
self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
let cell = cell as! InvitationTableViewCell
let snap = obj as! FDataSnapshot
if let status = snap.value["status"] as? String where status == InvitationStatus.pending.rawValue {
let userRef = Firebase(url: "\(kFBUsersPublicRef)/\(snap.key)")
userRef.observeSingleEventOfType(.Value, withBlock: {snapshot in
let user = User(key: snapshot.key, displayName: snapshot.value["displayName"] as! String, givenName: snapshot.value["givenName"] as! String, familyName: snapshot.value["familyName"] as! String)
cell.user = user
cell.delegate = self
})
}
}
self.tableView.dataSource = self.dataSource
self.tableView.delegate = self
}
任何人都可以帮助诊断我在这里做错了什么吗?我的方法正确吗?
感谢您提供的任何帮助!
我已经改变了我的方法来找到解决方案。我在 Firebase 中修改了我的数据结构,以便状态成为键。然后,我将 FirebaseTableViewDataSource 绑定到 'pending' 键的引用。当用户 accepts/rejects 邀请时,它会从 'pending' 节点中删除并在 'accepted'/'rejected' 节点上创建。当添加新 'pending' 邀请或现有 'pending' 邀请为 accepted/rejected.
时,tableView 现在会按预期更新