仅当 json 响应中的时间更新时,如何更改按钮图标?
仅当响应时间发生变化时,我才必须更改按钮图标。所以我创建了一个计时器函数,每 30 秒调用一次 api 并重新加载表。代码如下:
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var refreshed = false
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
table.delegate = self
self.getPosts()
timer = Timer.scheduledTimer(withTimeInterval: 30.0, repeats: true) { (timer) in
self.refreshed = true
self.getPosts()
}
}
//TableView Datasource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let data = posts[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" // Pass any format here you want according to your date response
let convDate = dateFormatter.date(from: data.postUpdatedAt ?? "")
if refreshed == false {
cell.timeBeforeRefreshInterval = convDate
}
else {
cell.timeAfterRefreshInterval = convDate
if cell.timeAfterRefreshInterval == cell.timeBeforeRefreshInterval {
cell.button.setImage(UIImage(named: "1"), for: .normal)
}
else {
cell.button.setImage(UIImage(named: "2"), for: .normal)
cell.timeBeforeRefreshInterval = cell.timeAfterRefreshInterval
refreshed = false
}
}
return cell
}
}
class HomeTableViewCell: UITableViewCell {
var timeBeforeRefreshInterval : Date?
var timeAfterRefreshInterval : Date?
}
首先,我创建了变量来在 HomeViewController 中存储日期响应,但它将始终存储创建的最后一个单元格的响应,因此我在 HomeTableVIewCell 中添加了这些变量。然而它仍然不起作用。是否有更好的逻辑来比较发布时间并据此更新您的单元格?我想做的就是比较时间,如果有人在群组中发布了某些内容,时间就会更新,我需要更改按钮图标以显示有人发布了新内容。
I have to change a button icon only if the time in response changes. So I created a timer function which hits api every 30 seconds and reloads table. Here is the code:
class HomeViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var refreshed = false
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
table.delegate = self
self.getPosts()
timer = Timer.scheduledTimer(withTimeInterval: 30.0, repeats: true) { (timer) in
self.refreshed = true
self.getPosts()
}
}
//TableView Datasource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let data = posts[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTableViewCell", for: indexPath) as! HomeTableViewCell
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" // Pass any format here you want according to your date response
let convDate = dateFormatter.date(from: data.postUpdatedAt ?? "")
if refreshed == false {
cell.timeBeforeRefreshInterval = convDate
}
else {
cell.timeAfterRefreshInterval = convDate
if cell.timeAfterRefreshInterval == cell.timeBeforeRefreshInterval {
cell.button.setImage(UIImage(named: "1"), for: .normal)
}
else {
cell.button.setImage(UIImage(named: "2"), for: .normal)
cell.timeBeforeRefreshInterval = cell.timeAfterRefreshInterval
refreshed = false
}
}
return cell
}
}
class HomeTableViewCell: UITableViewCell {
var timeBeforeRefreshInterval : Date?
var timeAfterRefreshInterval : Date?
}
First I created variable to store date response in HomeViewController
but it will always store the response of last cell created so I added those variables in HomeTableVIewCell
. However its still not working. Is there any better logic to compare times of post and update your cell according to that? All I wanna do is compare time, if someone posts something in group, time gets updated and I need to change button icon to show someone has posted something new.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论