简体   繁体   中英

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 ). 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.

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.

ex:

  • Cell 1 has to refresh every 1 min and do some work
  • Cell 2 has to refresh every 3 mins and do some work
  • Cell 3 has to refresh every 4 mins and do some work.

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 . Then assign each cell a timer from the array based on their NSIndexPath in the tableView.cellForRowAtIndexPath method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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