Reload Cell label x seconds

My TableView is powered from an array of Player objects.
Each player has an attribute for their currentPlayedTime.
My tableView I would therefore like the cells to refresh every minute or so just to display the players currentPlayedTime on that cell. I’m having a bit of trouble linking this all together.

I have a function called updateTime, its called from a timer. I then update all the players playing time by 1 second. (I’m only using 1 second for the cell update just to see it working quickly, rather than waiting a whole minute each time) This works fine, but when I come to update the cells I’m not sure how to fit it all together.

I thought it should be something like this:

let visibleCells = tableView.visibleCells as! [MyCustomTableViewCell]
for cell in visibleCells  {
   // update the label somehow
}

I’m not sure how make this cell instance get the currentPlayedTime of the Player it is showing. So not sure how to fit it all together.

also, If there is a better way to update these cells then advice gladly appreciated :slight_smile:

Check out this method of UITableView:
func indexPath(for cell: UITableViewCell) -> IndexPath?

You could use that in your “for cell in visibleCells” loop to get the indexPath of each visible cell. That should give you access to the desired entry in the table’s data source.

Have fun!

1 Like

Thanks @rcasey - That’s great!