patternswiftMinor
Countdown timer in every table cell
Viewed 0 times
cellcountdowneverytimertable
Problem
I am making countdown app with multiple records, so I am using UITableView to show that records and countdown dates. What I do is just take one timer and reload cells on every second. Is this a best approach to reload visible cells on every second or is there another better approach there?
updateCellContentsTimer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(ViewController.updateCells),
userInfo: nil, repeats: true)
// MARK: Custom Functions
func updateCells() {
let indexPathsArray = tableView.indexPathsForVisibleRows
for indexPath in indexPathsArray! {
let cell = tableView.cellForRow(at: indexPath) as! CountdownTableViewCell
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"
}
}Solution
to avoid DRY i would put this line (i think it is in your code only a sample logic) inside the cell. in this case you have the logic for calculating the remaining time inside the cell and not outside somewhere. and you can use always the same function to update the progress.
should be become
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"should be become
cell.updateProgress()Code Snippets
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"cell.updateProgress()Context
StackExchange Code Review Q#160628, answer score: 3
Revisions (0)
No revisions yet.