3 Replies
      Latest reply on Feb 10, 2019 2:21 PM by OOPer
      h.dale Level 1 Level 1 (0 points)

        I have the following loop in my code:

         

        for i in 0..<array.count where newRow != i {

           array{i].active = false

        }

         

        I would like to assign the selectedIndexPath.row to the variable newRow.  Is that possible or is there a better way to make the loop ignore the current row I am on?

         

        Thanks in advance for any help.

        • Re: Convert selectedindexpath.row to integer?
          OOPer Level 8 Level 8 (5,325 points)

          Please show more context. What is `selectedIndexPath`? Where and how you declare it? Especially, what is the type of it?

            • Re: Convert selectedindexpath.row to integer?
              h.dale Level 1 Level 1 (0 points)

              THIS IS THE CODE:

               

              if let todo = sourceViewController.todo {

                          if let selectedIndexPath =

                              tableView.indexPathForSelectedRow {

                              todos[selectedIndexPath.row] = todo

                              tableView.reloadRows(at: [selectedIndexPath],

                                                   with: .none)

               

              I WOULD LIKE TO ADD CODE HERE TO SET ACTIVATED TO TRUE IN THIS ROW AND FALSE IN ALL OTHER ROWS:

                              for i in 0..<todos.count where newRow != i {

                                  todos[i].activated = false

                              }

                • Re: Convert selectedindexpath.row to integer?
                  OOPer Level 8 Level 8 (5,325 points)

                  You are not showing enough info, so I may be missing something, but why won't you do it simply:

                                  for i in 0..<todos.count where selectedIndexPath.row != i {
                                      todos[i].activated = false
                                  }
                  
                  

                   

                  Or, when I want TO SET ACTIVATED TO TRUE IN THIS ROW AND FALSE IN ALL OTHER ROWS, I would write:

                                  for i in 0..<todos.count {
                                      todos[i].activated = (selectedIndexPath.row == i)
                                  }