Swift looping through array












0















I have a UITableView that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.



I have found that the following code works to disable a specific cell that I give it.



UITableViewCellSelectionStyle.none


Any help on this would be greatly appreciated.



This is how I am checking which cell is selected:



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!

let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)


}









share|improve this question

























  • I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }

    – ZacharyGeorge
    Nov 23 '18 at 21:24













  • So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?

    – Joakim Danielson
    Nov 23 '18 at 21:25













  • Joakim - correct

    – ZacharyGeorge
    Nov 23 '18 at 21:26











  • Don't use an array containing index paths. Add the selected or enabled information to your data model. The benefit is that the Ui will be reliably updated in cellForRow.

    – vadian
    Nov 23 '18 at 21:26








  • 1





    Why do you call indexPathForSelectedRow? That's pointless. The method gives you the index path in the indexPath parameter.

    – vadian
    Nov 23 '18 at 21:38
















0















I have a UITableView that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.



I have found that the following code works to disable a specific cell that I give it.



UITableViewCellSelectionStyle.none


Any help on this would be greatly appreciated.



This is how I am checking which cell is selected:



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!

let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)


}









share|improve this question

























  • I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }

    – ZacharyGeorge
    Nov 23 '18 at 21:24













  • So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?

    – Joakim Danielson
    Nov 23 '18 at 21:25













  • Joakim - correct

    – ZacharyGeorge
    Nov 23 '18 at 21:26











  • Don't use an array containing index paths. Add the selected or enabled information to your data model. The benefit is that the Ui will be reliably updated in cellForRow.

    – vadian
    Nov 23 '18 at 21:26








  • 1





    Why do you call indexPathForSelectedRow? That's pointless. The method gives you the index path in the indexPath parameter.

    – vadian
    Nov 23 '18 at 21:38














0












0








0








I have a UITableView that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.



I have found that the following code works to disable a specific cell that I give it.



UITableViewCellSelectionStyle.none


Any help on this would be greatly appreciated.



This is how I am checking which cell is selected:



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!

let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)


}









share|improve this question
















I have a UITableView that has 100 cells. I want to create an array that will hold index values of that table that I want to disable the table cells if the table row selected matches any of the values in the array.



I have found that the following code works to disable a specific cell that I give it.



UITableViewCellSelectionStyle.none


Any help on this would be greatly appreciated.



This is how I am checking which cell is selected:



override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


let selectedRow = tableView.indexPathForSelectedRow?.row
let workoutSelected = selectedRow
stringPassedTableView = workoutSelected!

let myVC = storyboard?.instantiateViewController(withIdentifier: "showWorkout") as! WorkoutViewController
myVC.stringPassed = stringPassedTableView
navigationController?.pushViewController(myVC, animated: true)


}






ios swift loops uitableview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 21:35







ZacharyGeorge

















asked Nov 23 '18 at 21:22









ZacharyGeorgeZacharyGeorge

32




32













  • I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }

    – ZacharyGeorge
    Nov 23 '18 at 21:24













  • So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?

    – Joakim Danielson
    Nov 23 '18 at 21:25













  • Joakim - correct

    – ZacharyGeorge
    Nov 23 '18 at 21:26











  • Don't use an array containing index paths. Add the selected or enabled information to your data model. The benefit is that the Ui will be reliably updated in cellForRow.

    – vadian
    Nov 23 '18 at 21:26








  • 1





    Why do you call indexPathForSelectedRow? That's pointless. The method gives you the index path in the indexPath parameter.

    – vadian
    Nov 23 '18 at 21:38



















  • I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }

    – ZacharyGeorge
    Nov 23 '18 at 21:24













  • So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?

    – Joakim Danielson
    Nov 23 '18 at 21:25













  • Joakim - correct

    – ZacharyGeorge
    Nov 23 '18 at 21:26











  • Don't use an array containing index paths. Add the selected or enabled information to your data model. The benefit is that the Ui will be reliably updated in cellForRow.

    – vadian
    Nov 23 '18 at 21:26








  • 1





    Why do you call indexPathForSelectedRow? That's pointless. The method gives you the index path in the indexPath parameter.

    – vadian
    Nov 23 '18 at 21:38

















I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }

– ZacharyGeorge
Nov 23 '18 at 21:24







I should also note that I found that I can accomplish this by giving it a value: if(indexPath.row == 2) { UITableViewCellSelectionStyle.none }

– ZacharyGeorge
Nov 23 '18 at 21:24















So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?

– Joakim Danielson
Nov 23 '18 at 21:25







So if you need to disable cells in row 0, 5, 13, 67 then you need an array that contains 0, 5, 13, 67?

– Joakim Danielson
Nov 23 '18 at 21:25















Joakim - correct

– ZacharyGeorge
Nov 23 '18 at 21:26





Joakim - correct

– ZacharyGeorge
Nov 23 '18 at 21:26













Don't use an array containing index paths. Add the selected or enabled information to your data model. The benefit is that the Ui will be reliably updated in cellForRow.

– vadian
Nov 23 '18 at 21:26







Don't use an array containing index paths. Add the selected or enabled information to your data model. The benefit is that the Ui will be reliably updated in cellForRow.

– vadian
Nov 23 '18 at 21:26






1




1





Why do you call indexPathForSelectedRow? That's pointless. The method gives you the index path in the indexPath parameter.

– vadian
Nov 23 '18 at 21:38





Why do you call indexPathForSelectedRow? That's pointless. The method gives you the index path in the indexPath parameter.

– vadian
Nov 23 '18 at 21:38












3 Answers
3






active

oldest

votes


















0














You can use the following tableview delegate method to allow selection or not



tableView:willSelectRowAtIndexPath:


Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.



You can see the documentation here:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value






share|improve this answer































    0














    A much better way that, create an array of your model class which data you are passing into tableView data source.



    Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.



    Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.



    Hope you understand !!






    share|improve this answer































      0














      You have an array (or set) as a class property



      var disabledRows = [IndexPaath]()


      then simply use it at the start of the func



      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      if disabledRows.contains(indexPath) { return }

      // code to handle row with enabled cell
      }





      share|improve this answer























        Your Answer






        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: "1"
        };
        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',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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%2fstackoverflow.com%2fquestions%2f53453061%2fswift-looping-through-array%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        You can use the following tableview delegate method to allow selection or not



        tableView:willSelectRowAtIndexPath:


        Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.



        You can see the documentation here:
        https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value






        share|improve this answer




























          0














          You can use the following tableview delegate method to allow selection or not



          tableView:willSelectRowAtIndexPath:


          Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.



          You can see the documentation here:
          https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value






          share|improve this answer


























            0












            0








            0







            You can use the following tableview delegate method to allow selection or not



            tableView:willSelectRowAtIndexPath:


            Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.



            You can see the documentation here:
            https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value






            share|improve this answer













            You can use the following tableview delegate method to allow selection or not



            tableView:willSelectRowAtIndexPath:


            Simply check if the cell index is in your disabled cell indexes and if so return nil. Otherwise return the indexpath.



            You can see the documentation here:
            https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614943-tableview?language=objc#return-value







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 1:48









            jmsjms

            33718




            33718

























                0














                A much better way that, create an array of your model class which data you are passing into tableView data source.



                Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.



                Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.



                Hope you understand !!






                share|improve this answer




























                  0














                  A much better way that, create an array of your model class which data you are passing into tableView data source.



                  Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.



                  Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.



                  Hope you understand !!






                  share|improve this answer


























                    0












                    0








                    0







                    A much better way that, create an array of your model class which data you are passing into tableView data source.



                    Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.



                    Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.



                    Hope you understand !!






                    share|improve this answer













                    A much better way that, create an array of your model class which data you are passing into tableView data source.



                    Add a property into model class like named as "isSelectable" and assign it properly when you are parsing your model class data.



                    Use this "isSelectable" property, when you will select a row then check this property it is true or false and performs operation accordingly.



                    Hope you understand !!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 6:05









                    iDev750iDev750

                    5961316




                    5961316























                        0














                        You have an array (or set) as a class property



                        var disabledRows = [IndexPaath]()


                        then simply use it at the start of the func



                        override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                        if disabledRows.contains(indexPath) { return }

                        // code to handle row with enabled cell
                        }





                        share|improve this answer




























                          0














                          You have an array (or set) as a class property



                          var disabledRows = [IndexPaath]()


                          then simply use it at the start of the func



                          override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                          if disabledRows.contains(indexPath) { return }

                          // code to handle row with enabled cell
                          }





                          share|improve this answer


























                            0












                            0








                            0







                            You have an array (or set) as a class property



                            var disabledRows = [IndexPaath]()


                            then simply use it at the start of the func



                            override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                            if disabledRows.contains(indexPath) { return }

                            // code to handle row with enabled cell
                            }





                            share|improve this answer













                            You have an array (or set) as a class property



                            var disabledRows = [IndexPaath]()


                            then simply use it at the start of the func



                            override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
                            if disabledRows.contains(indexPath) { return }

                            // code to handle row with enabled cell
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 13:39









                            Joakim DanielsonJoakim Danielson

                            9,0553724




                            9,0553724






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • 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%2fstackoverflow.com%2fquestions%2f53453061%2fswift-looping-through-array%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

                                Create new schema in PostgreSQL using DBeaver

                                Deepest pit of an array with Javascript: test on Codility

                                Costa Masnaga