Swift 在应用程序之间切换时未调用CellForRowAt(内部为gif)
我有一个ChatViewController和聊天信息。我使用序列图像板约束和标识符初始化消息气泡的布局,以通过代码更改它们:Swift 在应用程序之间切换时未调用CellForRowAt(内部为gif),swift,uitableview,Swift,Uitableview,我有一个ChatViewController和聊天信息。我使用序列图像板约束和标识符初始化消息气泡的布局,以通过代码更改它们: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = chatUser.dequeueReusableCell(withIdentifier: "chatUserCell", for: index
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”布尔值更改约束:
它的工作原理与您在.gif中看到的一样,但当我更改应用程序并重新打开我的应用程序时,cellForRowAt不会被调用,我将丢失约束,视图将使用初始情节提要约束:
您可以在视图上调用tableView.delegate=self&tableView.dataSource=self您应该收听应用程序进入前台的通知,即
ui应用程序。willEnterForegroundNotification
在viewDidLoad()
中,添加观察者:
NotificationCenter.default.addObserver(self, selector: #selector(resumeState), name: UIApplication.willEnterForegroundNotification, object: nil)
然后声明resumeState()
函数,当应用程序进入前台状态时将调用该函数
@objc func resumeState() {
yourTableView.reloadData()
}
也不要忘记在Denit中移除观察者
deinit {
print("\(self.description) deinitialized")
NotificationCenter.default.removeObserver(self)
}
您是否尝试在
视图中调用tableview重新加载将出现
当cellForRow从后台调用时,它似乎没有被调用。我建议您使用两个不同的单元格,一个用于我们的用户,另一个用于另一个用户您是否尝试在AppDelegate.swift文件中调用ApplicationWilenterForeground?重新打开应用程序时将调用此函数。因为当您从后台重新打开应用程序时,viewdidload和ViewWillExample函数将不会被调用。@JohnDole如果tableView在视图中显示,您是否检查了图层?
deinit {
print("\(self.description) deinitialized")
NotificationCenter.default.removeObserver(self)
}