首页 > 解决方案 > 数据更改后刷新视图

问题描述

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

我已将子视图添加到 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!)
    }

}

}

标签: iosswift

解决方案


将字符串变量分配给 aUILabelattributedText属性不会在该字符串和标签之间创建绑定;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
}

推荐阅读