gpt4 book ai didi

ios - 数据更改后刷新 View

转载 作者:行者123 更新时间:2023-11-30 11:00:16 24 4
gpt4 key购买 nike

我正在尝试进行实时聊天,进展顺利,但我遇到了问题。

我已经向navigationController 添加了一个 subview 而不是标题。我在这个 View 中有一个头像、用户全名和状态,就像 Whatsapp 一样。我从推送服务获得了用户在线字符串,我想将其显示在用户全名(例如 Whatsapp)的 subview 底部。当我从 func onPresenceChanged(stateChange method ) 获取在线字符串时,如何刷新 View ?

我添加了 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!)
}

}

}

最佳答案

将字符串变量分配给 UILabelattributedText 属性不会在该字符串和标签之间创建绑定(bind);如果您想在 UI 中看到更新的值,则需要更新 attributedText。另外,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
}

关于ios - 数据更改后刷新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53464115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com