iOS 7’s Subtle UITableView View Hierarchy Changes

While updating one of my oldest apps to iOS 7, I noticed that I was getting an exception when trying to grab a UITableViewCell from a UITableView. The code looked something like this:

- (IBAction)OnSomeReallyImportantStepperChanged:(id)sender
{
    UIStepper* stepper = sender;

    UITableViewCell* cell = (UITableViewCell*)[[stepper superview] superview];
    UITableView* table = (UITableView *)[cell superview];
    NSIndexPath* pathOfTheCell = [table indexPathForCell:cell]; // ***BOOM***

This code worked in iOS 6 and below, but threw an exception in iOS 7:

[UITableViewCell indexPathForCell:]: unrecognized selector sent to instance

What’s going on here? Well, a quick debugging session made it quite clear. cell is actually a UITableViewCellScrollView, not a UITableViewCell, and table is really a UITableViewCell, so naturally it didn’t have an indexPathForCell method.

The moral of the story is – don’t make too many assumptions about the view hierarchy, because it may change!

Leave a Reply

Your email address will not be published. Required fields are marked *