【发布时间】:2018-07-26 09:35:28
【问题描述】:
我有一个在线/离线状态的有效 firebase 聊天应用程序,我只想知道如何在不重新加载表格视图的情况下获取这些数据。 当用户离线时,我必须刷新我的视图才能注意到更改。
还有如何在不重新加载视图的情况下更改导航字幕视图
我的用户在线和离线方法
func userOnline(UserId: String){
let uid = Auth.auth().currentUser?.uid
let myConnectionRef =
Database.database().reference().child("Users").child(UserId)
myConnectionRef.child("online").setValue(true)
myConnectionRef.child("last_online").setValue(Date().timeIntervalSince1970)
myConnectionRef.child("last_online").setValue(NSNumber(value: Int(NSDate().timeIntervalSince1970)))
}
func userOffline(UserId: String){
let myConnectionRef = Database.database().reference().child("Users").child(UserId)
myConnectionRef.child("online").setValue(false)
myConnectionRef.child("last_online").setValue(Date().timeIntervalSince1970)
myConnectionRef.child("last_online").setValue(NSNumber(value: Int(NSDate().timeIntervalSince1970)))
}
func checkUserStatus(userid:String){
let myConnectionRef = Database.database().reference().child("Users").child(userid)
myConnectionRef.child("online").setValue(true)
myConnectionRef.child("typing").setValue(false)
myConnectionRef.child("last_online").setValue(NSNumber(value: Int(NSDate().timeIntervalSince1970)))
// Observe For User logged in or logged out
myConnectionRef.observe(.childChanged) { (snapshot) in
guard let connected = snapshot.value as? Bool, connected else {return}
}
}
我使用 if else 语句来更改 cell.detailtext
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell
let user = users[indexPath.row]
cell.textLabel?.text = user.userName
let uid = Auth.auth().currentUser?.uid
if (user.online as? Bool)!{
cell.detailTextLabel?.font = UIFont.italicSystemFont(ofSize: 12)
cell.detailTextLabel?.textColor = UIColor.flatGreen()
cell.detailTextLabel?.text = "online"
}
else {
let date = user.last_online!
let seconds = user.last_online?.doubleValue
let timeStamp = NSDate(timeIntervalSince1970: seconds!)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yy hh:mm:a"
cell.detailTextLabel?.font = UIFont.italicSystemFont(ofSize: 12)
cell.detailTextLabel?.textColor = UIColor.lightGray
cell.detailTextLabel?.text = ("Last Seen: \(dateFormatter.string(from: timeStamp as Date))")
}
if let profileImageUrl = user.profileImageUrl {
cell.profileImageView.loadImageFromCache(urlString: profileImageUrl)
}
return cell
}
但我必须刷新 tableview。 有没有办法让这个自动改变
【问题讨论】:
-
你能发布完整的
cellForRow代码吗? -
是的,我更新了我的
cellForRow -
好的,据我了解,只要该特定用户的在线状态发生变化,您就需要在另一个用户的设备中更改该用户的状态,而无需重新加载整个表格视图。每当用户状态发生变化时,您都可以尝试使用推送通知(首选静音,但也可以进行远程)。
-
你能告诉我怎么做吗?
-
好的,给我一些时间,长代码,将作为答案发布。
标签: swift firebase firebase-realtime-database