当用户单击表格视图中的单元格时,它会更改颜色,但是当他们刷新应用程序(例如注销并重新登录或在其iPhone上向上滑动)时,该单元格会变回原始颜色-是否可以使其在用户刷新应用程序时如此,用户单击的单元格仍保持更改后的颜色以显示它已被打开?
更新:
视图控制器中的代码
extension NotificationViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return notifications.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "NotificationTableViewCell", for: indexPath) as! NotificationTableViewCell
let notification = notifications[indexPath.row]
let user = users[indexPath.row]
cell.notification = notification
cell.user = user
cell.delegate = self
cell.backgroundColor = UIColor(red: 245, green: 245, blue: 245)
return cell
}
}
UITableViewCell中的代码
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
let cellBackground = UIView()
cellBackground.backgroundColor = UIColor(red: 245, green: 245, blue: 245)
UITableViewCell.appearance().selectedBackgroundView = cellBackground
}
}
更新:
JSON firebase结构的片段:
"notification" : {
"CjeP35ceAQZJuUPhm7U1eF3Yq4F3" : {
"-La5XJZV059J-fcbH4Px" : {
"from" : "Gmg1ojNoBiedFPRNSL4sBZz2gSx2",
"objectId" : "-La5XJYuWm6dLfGnte2h",
"timestamp" : 1552740338,
},
"-La6KbtcG7VIIk7JsBvT" : {
"from" : "Gmg1ojNoBiedFPRNSL4sBZz2gSx2",
"objectId" : "-La6KbtbAAAPjeo98dGe",
"timestamp" : 1552753786,
},
"FoFQDAGGX9hntBiBdXYCBHd8yas2" : {
"-La6Kb4DyACBRihJf4c1" : {
"from" : "Gmg1ojNoBiedFPRNSL4sBZz2gSx2",
"objectId" : "-La6Kb42zWtwcAVlubK7",
"timestamp" : 1552753783,
},