gpt4 book ai didi

swift - 在应用程序之间切换时未调用 CellForRowAt(内部 gif)

转载 作者:行者123 更新时间:2023-12-01 12:03:35 24 4
gpt4 key购买 nike

我有一个 ChatViewController 和聊天消息。我使用 Storyboard 约束和标识符初始化消息气泡的布局,以通过代码更改它们:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = chatUser.dequeueReusableCell(withIdentifier: "chatUserCell", for: indexPath) as! ChatUserTableViewCell
print("cellforrowcalled")
cell.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi))

// I check here the message sender
cell.isFromCurrentUser = messages[indexPath.row].fromUid == UserApi.shared.CURRENT_USER_UID!

cell.labelChatMessage.text = messages[indexPath.row].chatText

cell.labelChatMessageDate.text = messages[indexPath.row].chatDate?.dateValue().timeAgo(numericDates: false)

return cell
}

在我的 ChatCellViewController 中,我根据“isFromCurrentUser” bool 值更改约束:

var isFromCurrentUser: Bool! {
didSet {
labelChatMessage.backgroundColor = isFromCurrentUser ? .systemBlue : .darkGray

if isFromCurrentUser {
constraintOtherUserTimestamp.isActive = false
constraintOtherUserLeading.isActive = false
constraintOtherUserTrailing.isActive = false
constraintCurrentUserTimestamp.isActive = true
constraintCurrentUserLeading.isActive = true
constraintCurrentUserTrailing.isActive = true
} else {
constraintCurrentUserTimestamp.isActive = false
constraintCurrentUserLeading.isActive = false
constraintCurrentUserTrailing.isActive = false
constraintOtherUserTimestamp.isActive = true
constraintOtherUserLeading.isActive = true
constraintOtherUserTrailing.isActive = true
}
}
}

它的工作原理与您在 .gif 中看到的一样,但是当我更改应用程序并重新打开我的应用程序时,未调用 cellForRowAt,我失去​​了约束并且 View 正在使用初始 Storyboard约束:

enter image description here

最佳答案

您应该收听应用程序进入前台的通知,即 UIApplication.willEnterForegroundNotification

在您的 viewDidLoad() 中,添加观察者:

NotificationCenter.default.addObserver(self, selector: #selector(resumeState), name: UIApplication.willEnterForegroundNotification, object: nil)

然后声明resumeState()函数,当应用程序进入前台状态时将调用此函数。

@objc func resumeState() {
yourTableView.reloadData()
}

也不要忘记在 deinit 中删除观察者

deinit {
print("\(self.description) deinitialized")
NotificationCenter.default.removeObserver(self)
}

关于swift - 在应用程序之间切换时未调用 CellForRowAt(内部 gif),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59894844/

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