简体   繁体   English

在Swift中以不同的间隔刷新TableView单元格以从URL获取数据

[英]Refresh TableView Cell at different intervals in Swift to get data from URL

I have a tableView controller in my swift app which when loaded, each cell sends a http request to a URL (ex: https://helloworld.com/sendStatus.txt ). 我的swift应用程序中有一个tableView控制器,在加载时,每个单元格都会向URL发送一个http请求(例如: https : //helloworld.com/sendStatus.txt )。 say the link displays raw content of status of server (OK or Critical). 说该链接显示服务器状态的原始内容(“正常”或“严重”)。 If ok I turn the cell bg color to Green else Red. 如果可以,我将单元格bg颜色变为绿色,否则为红色。

Problem (In Swift) : Each cell is assigned with a diff URL and refresh rate(variable in custom cell object), I would like to set a timer to each cell and change the bg color according to the status returned from the URL. 问题(在Swift中) :每个单元格都分配有一个差异URL和刷新率(在自定义单元格对象中可变),我想为每个单元格设置一个计时器,并根据URL返回的状态更改背景色。

ex: 例如:

  • Cell 1 has to refresh every 1 min and do some work 单元1必须每1分钟刷新一次并做一些工作
  • Cell 2 has to refresh every 3 mins and do some work 单元2必须每3分钟刷新一次并做一些工作
  • Cell 3 has to refresh every 4 mins and do some work. 单元3必须每4分钟刷新一次并做一些工作。

How do achieve the timer and refresh for each cell which has different refresh rates. 如何实现计时器并刷新具有不同刷新率的每个单元。

Please help me with theoretical explanation or sample snippets. 请帮助我提供理论解释或样本片段。

Thanks in advance. 提前致谢。

You can create an array (1D if you only have one section) of NSTimer . 您可以创建一个NSTimer数组(如果只有一个部分,则为1D)。 Then assign each cell a timer from the array based on their NSIndexPath in the tableView.cellForRowAtIndexPath method. 然后,根据tableView.cellForRowAtIndexPath方法中的NSIndexPath ,为每个单元从数组中分配一个计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM