Countdown timer in every table cell
up vote
4
down vote
favorite
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"
}
}
swift ios timer swift3
add a comment |
up vote
4
down vote
favorite
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"
}
}
swift ios timer swift3
Instead of reloading every cell on timer event you can use the notification mechanism of Cocoa.
– Shial
Apr 14 '17 at 1:28
1
@Shial, since it's a countdown, every visible cell needs to change every second. Doing that indirectly through notifications only adds complexity without avoiding the cell updates. The OP already limits the updates to visible cells so the process is optimal.
– Alain T.
Apr 19 '17 at 13:22
@AlainT. so this is the right way to update time in every cell .. right??
– EI Captain v2.0
Apr 21 '17 at 4:44
1
Yes. What makes it right is that you use indexPathsForVisibleRows. This is the smallest number of cell updates that you can do for your countdown. Having only one timer to manage all the countdowns is also the optimal way to do it.
– Alain T.
Apr 21 '17 at 4:54
@AlainT. thanks buddy ... thanks for your suggestion .. really appreciate :)
– EI Captain v2.0
Apr 21 '17 at 5:00
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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"
}
}
swift ios timer swift3
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"
}
}
swift ios timer swift3
swift ios timer swift3
edited Jun 21 '17 at 0:04
200_success
127k15148410
127k15148410
asked Apr 13 '17 at 9:02
EI Captain v2.0
12114
12114
Instead of reloading every cell on timer event you can use the notification mechanism of Cocoa.
– Shial
Apr 14 '17 at 1:28
1
@Shial, since it's a countdown, every visible cell needs to change every second. Doing that indirectly through notifications only adds complexity without avoiding the cell updates. The OP already limits the updates to visible cells so the process is optimal.
– Alain T.
Apr 19 '17 at 13:22
@AlainT. so this is the right way to update time in every cell .. right??
– EI Captain v2.0
Apr 21 '17 at 4:44
1
Yes. What makes it right is that you use indexPathsForVisibleRows. This is the smallest number of cell updates that you can do for your countdown. Having only one timer to manage all the countdowns is also the optimal way to do it.
– Alain T.
Apr 21 '17 at 4:54
@AlainT. thanks buddy ... thanks for your suggestion .. really appreciate :)
– EI Captain v2.0
Apr 21 '17 at 5:00
add a comment |
Instead of reloading every cell on timer event you can use the notification mechanism of Cocoa.
– Shial
Apr 14 '17 at 1:28
1
@Shial, since it's a countdown, every visible cell needs to change every second. Doing that indirectly through notifications only adds complexity without avoiding the cell updates. The OP already limits the updates to visible cells so the process is optimal.
– Alain T.
Apr 19 '17 at 13:22
@AlainT. so this is the right way to update time in every cell .. right??
– EI Captain v2.0
Apr 21 '17 at 4:44
1
Yes. What makes it right is that you use indexPathsForVisibleRows. This is the smallest number of cell updates that you can do for your countdown. Having only one timer to manage all the countdowns is also the optimal way to do it.
– Alain T.
Apr 21 '17 at 4:54
@AlainT. thanks buddy ... thanks for your suggestion .. really appreciate :)
– EI Captain v2.0
Apr 21 '17 at 5:00
Instead of reloading every cell on timer event you can use the notification mechanism of Cocoa.
– Shial
Apr 14 '17 at 1:28
Instead of reloading every cell on timer event you can use the notification mechanism of Cocoa.
– Shial
Apr 14 '17 at 1:28
1
1
@Shial, since it's a countdown, every visible cell needs to change every second. Doing that indirectly through notifications only adds complexity without avoiding the cell updates. The OP already limits the updates to visible cells so the process is optimal.
– Alain T.
Apr 19 '17 at 13:22
@Shial, since it's a countdown, every visible cell needs to change every second. Doing that indirectly through notifications only adds complexity without avoiding the cell updates. The OP already limits the updates to visible cells so the process is optimal.
– Alain T.
Apr 19 '17 at 13:22
@AlainT. so this is the right way to update time in every cell .. right??
– EI Captain v2.0
Apr 21 '17 at 4:44
@AlainT. so this is the right way to update time in every cell .. right??
– EI Captain v2.0
Apr 21 '17 at 4:44
1
1
Yes. What makes it right is that you use indexPathsForVisibleRows. This is the smallest number of cell updates that you can do for your countdown. Having only one timer to manage all the countdowns is also the optimal way to do it.
– Alain T.
Apr 21 '17 at 4:54
Yes. What makes it right is that you use indexPathsForVisibleRows. This is the smallest number of cell updates that you can do for your countdown. Having only one timer to manage all the countdowns is also the optimal way to do it.
– Alain T.
Apr 21 '17 at 4:54
@AlainT. thanks buddy ... thanks for your suggestion .. really appreciate :)
– EI Captain v2.0
Apr 21 '17 at 5:00
@AlainT. thanks buddy ... thanks for your suggestion .. really appreciate :)
– EI Captain v2.0
Apr 21 '17 at 5:00
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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.
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"
should be become
cell.updateProgress()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"
should be become
cell.updateProgress()
add a comment |
up vote
0
down vote
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.
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"
should be become
cell.updateProgress()
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"
should be become
cell.updateProgress()
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.
cell.timeLeftLabel.text = progress + "hours:minutes:seconds"
should be become
cell.updateProgress()
answered Apr 21 '17 at 22:05
muescha
1665
1665
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f160628%2fcountdown-timer-in-every-table-cell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Instead of reloading every cell on timer event you can use the notification mechanism of Cocoa.
– Shial
Apr 14 '17 at 1:28
1
@Shial, since it's a countdown, every visible cell needs to change every second. Doing that indirectly through notifications only adds complexity without avoiding the cell updates. The OP already limits the updates to visible cells so the process is optimal.
– Alain T.
Apr 19 '17 at 13:22
@AlainT. so this is the right way to update time in every cell .. right??
– EI Captain v2.0
Apr 21 '17 at 4:44
1
Yes. What makes it right is that you use indexPathsForVisibleRows. This is the smallest number of cell updates that you can do for your countdown. Having only one timer to manage all the countdowns is also the optimal way to do it.
– Alain T.
Apr 21 '17 at 4:54
@AlainT. thanks buddy ... thanks for your suggestion .. really appreciate :)
– EI Captain v2.0
Apr 21 '17 at 5:00