6 Replies
      Latest reply on Jun 16, 2017 3:23 PM by QuinceyMorris
      sleipn1r Level 1 Level 1 (0 points)

        Hi,

         

        in Xcode 8.3 works this to get the selected switch changeValue fine

         

         

        ----

          let indexPath :NSIndexPath = (self.superview!.superview as! UITableView).indexPath(for: self)! as NSIndexPath

            

                let thisIsTheCell = localDev.allArtikeliPadObj[indexPath.row]

            

        --------

         

         

        in Xcode 9 crash my App with this Error after select any switch in any cell

         

         

         

        Could not cast value of type 'UIView'  to 'UITableView'



        ----




        have

        custom TableViewCell class > UITableView

        • Re: indexpath for cell TableView
          KMT Level 9 Level 9 (15,495 points)

          Option clean the build, then go again.

           

          Still no joy, delete and redo the wired up connections for that view via it's storyboard.

          • Re: indexpath for cell TableView
            QuinceyMorris Level 8 Level 8 (6,050 points)

            You cannot make any assumptions about the view hierarchy between the cell view and the table view, so this kind of thing:

             

                 self.superview!.superview as! UITableView
            
            

             

            is fragile, and likely to break.

             

            You'll have to find a different way to get to the table view at that point in the code.

             

            edit:

             

            If you're not in a delegate method (where the view is passed as a parameter) and you can't set up an @IBOutlet to the table view, you give the table view a tag value in IB, then use "viewWithTag(_:)" on the root view to search for the table view.

              • Re: indexpath for cell TableView
                sleipn1r Level 1 Level 1 (0 points)

                it works...

                 

                thanks...

                 

                 

                let indexPath :NSIndexPath = (self.superview!.viewWithTag(34)as! UITableView).indexPath(for: self)! as NSIndexPath

                  • Re: indexpath for cell TableView
                    QuinceyMorris Level 8 Level 8 (6,050 points)

                    It's not obvious that going up one level (superview.viewWithTag) is enough to future-proof this test. I suggest you go all the up to the window's root view (or even the UIWindow itself). At least go up as far as the enclosing view controller, and use "viewController.view.viewWithTag".

                     

                    The reason it's different in different versions of iOS is that Apple rearranges how its public views are organized within a larger view hierarchy. From one iOS version to another, the view you want may move up or down with private views that you're not allowed to touch.

                • Re: indexpath for cell TableView
                  Claude31 Level 8 Level 8 (8,465 points)

                  I don't know how about your design, but you probably have a limited list of tableViews in your class.

                   

                  So, an idea I have not tested:

                  for each UITableView, explore its subViews ; if one of them is the cell type you look for (could check with its Identifier), then you have found the tableView you're in.

                  • Re: indexpath for cell TableView
                    sleipn1r Level 1 Level 1 (0 points)

                    this is strange... because

                    in 8.3 works fine...

                     

                     

                    now have a bunch of cells and get the cell like this

                     

                    --

                    UITableviewStuff...

                    cellForRowAtIndexPath...

                    ..

                      let posCell = self.openPosTable.dequeueReusableCell(withIdentifier: "posCell", for: indexPath as IndexPath) as!  posCell

                    --

                    all cells are visible and scroll is OK

                     

                    my cell has 2 switches

                     

                    in my custom Cell Class have switch action to changeVal method.

                     

                    but

                    (How can i grab the Index object in the Cell Class in the UITableview) ????

                    old code is

                     

                    tableviewclass switch action method is

                    let indexPath :NSIndexPath = (self.superview!.superview as! UITableView).indexPath(for: self)! as NSIndexPath

                     

                            let thisIsTheCell = localDev.allArtikeliPadObj[indexPath.row]

                     

                    my App crash with reason see above...

                     

                    but the Breakpoint (Exception Breakpoint) or ALL ERROR does not stop the Breakpoint...

                     

                    the App hang on tap any switch on any row.