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"
}
}









share|improve this question
















bumped to the homepage by Community 12 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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















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"
}
}









share|improve this question
















bumped to the homepage by Community 12 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • 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













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"
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 21 '17 at 0:04









200_success

128k15149412




128k15149412










asked Apr 13 '17 at 9:02









EI Captain v2.0

12114




12114





bumped to the homepage by Community 12 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 12 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • 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






  • 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










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





share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























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





    share|improve this answer

























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





      share|improve this answer























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





        share|improve this answer












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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 21 '17 at 22:05









        muescha

        1765




        1765






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Code Review Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Ellipse (mathématiques)

            Quarter-circle Tiles

            Mont Emei