仅当 json 响应中的时间更新时,如何更改按钮图标?

发布于 2025-01-16 06:23:44 字数 2064 浏览 0 评论 0原文

仅当响应时间发生变化时,我才必须更改按钮图标。所以我创建了一个计时器函数,每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文