ios - 数据更改后刷新视图
问题描述
我正在尝试进行实时聊天,进展顺利,但我遇到了问题。
我已将子视图添加到 navigationController 而不是标题。我在这个视图中有一个头像、用户的全名和状态,比如 whatsapp。我有来自推送服务的用户是在线字符串,我想在我的用户全名的子视图底部显示它,比如whatsapp。从 func onPresenceChanged(stateChange method 获取在线字符串时如何刷新视图?
我添加了 self.navbar.setNeedsDisplay() 但它不起作用。
ChatViewController.swift(我剪掉了它以便快速查看)
class ChatViewController: UIViewController, UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PCRoomDelegate {
var navbarView = UIView()
var navbarAvatar = UIImageView()
var navbar = UILabel()
var statusString = [String]()
var usernameString = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.navbarView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4, height: 44.0))
self.navbarView.backgroundColor = UIColor.clear
self.navbar = UILabel(frame: CGRect(x: 44.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4 - 44, height: 44.0))
self.navbar.backgroundColor = UIColor.clear
self.navbar.numberOfLines = 2
self.navbar.textAlignment = NSTextAlignment.left
let bodyText = NSMutableAttributedString(string: "Halil İbrahim YÜCE", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
usernameString.append("\n@" + "halilyc")
bodyText.append(NSAttributedString(string: usernameString.last!, attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13), NSAttributedString.Key.foregroundColor : UIColor.gray]))
self.navbar.attributedText = bodyText
self.navbarView.addSubview(navbar)
self.navigationItem.titleView = self.navbarView
}
func onPresenceChanged(
stateChange: PCPresenceStateChange,
user: PCUser
) {
print("User \(user.displayName)'s presence changed to \(stateChange.current.rawValue)")
self.statusString.append(stateChange.current.rawValue)
self.navbar.setNeedsDisplay()
if statusString.count != 0 {
self.usernameString.append("\n@" + statusString.last!)
}
}
}
解决方案
将字符串变量分配给 aUILabel
的attributedText
属性不会在该字符串和标签之间创建绑定;attributedText
如果您想在 UI 中看到更新的值,则需要更新。此外,UI 更新必须在主线程上执行;您来自网络的更新将在后台线程上传递。
最后,我认为不需要保留一系列状态,您只需要最新的。
您可以重构以创建一个计算变量,为导航栏提供当前用户状态文本。
目前尚不清楚您打算如何显示显示名称、用户名和状态(有 3 件事,但只有 2 行)。我刚刚做了三行,你可以根据需要进行调整。
class ChatViewController: UIViewController, UITextFieldDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, PCRoomDelegate {
var navbarView: UIView!
var navbarAvatar: UIImageView!
var navbar: UILabel!
var status: String? {
didSet {
self.updateNavText()
}
var username: String? {
didSet {
self.updateNavText()
}
var displayName: String?
didSet {
self.updateNavText()
}
var statusText: NSAttributedString {
let text = NSMutableAttributedString(string: displayName ?? "", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
if let username = self.username {
text.append("\n@\(username)")
}
if let status = self.status {
text.append("\n\(status)")
}
return text
}
override func viewDidLoad() {
super.viewDidLoad()
self.navbarView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4, height: 44.0))
self.navbarView.backgroundColor = UIColor.clear
self.navbar = UILabel(frame: CGRect(x: 44.0, y: 0.0, width: UIScreen.main.bounds.width / 1.4 - 44, height: 44.0))
self.navbar.backgroundColor = UIColor.clear
self.navbar.numberOfLines = 2
self.navbar.textAlignment = NSTextAlignment.left
let bodyText = NSMutableAttributedString(string: "Halil İbrahim YÜCE", attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .bold)])
self.updateNavText()
self.navbarView.addSubview(navbar)
self.navigationItem.titleView = self.navbarView
}
func onPresenceChanged(
stateChange: PCPresenceStateChange,
user: PCUser
) {
print("User \(user.displayName)'s presence changed to \(stateChange.current.rawValue)")
DispatchQueue.main.async {
self.status = stateChange.current.rawValue
}
}
func updateNavText() {
self.navbar.attributedText = self.statusText
}
推荐阅读
- pandas - 按国家/地区查找平均击球率(“Ave”)和总世纪数,但仅包括起始年份为 2010 年或更晚的记录
- python - 如何将 ["3", ".", "1"] 更改为 [3.1]
- jenkins - 如何按计划自动缩减 OpenShift/Kubernetes pod?
- php - 在准备执行 SQL 查询时如何处理或格式化数据和时间数据类型?
- java - 如何在创建的 Excel 工作簿中创建工作表?
- r - 在 R 中加入多个 OR 语句
- android - PublishSubject 与 Kotlin 协程(流程)
- c# - [BotFramework]:如何解决:欢迎消息在 V4 开发的 C# WebChatBot 中没有显示给用户,但在模拟器中显示?
- reactjs - 如何在 makeStyles 中使用“主题”和“道具”?
- javascript - 为什么我的数组在字典值的另一个数组中?