Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8: -[UITableViewWrapperView textField]: unrecognized selector sent to instance

Hello: I've been testing my app on iOS 6, 7, and now 8 (beta 5). My UITableView with custom UITableViewCells is working fine on 6 and 7. However, on iOS 8, I'm getting a crash when I attempt to access a subview (text field) of a cell.

I am aware of the fact that there's another view in the cell's hierarchy in iOS 7. Strangely, it appears that this isn't the case in iOS 8. Here's the code I'm using:

    //Get the cell
    CustomCell *cell = nil;

    //NOTE: GradingTableViewCell > UITableViewCellScrollView (iOS 7+ ONLY) > UITableViewCellContentView > UIButton (sender)
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
        cell = (CustomCell *)sender.superview.superview;
    } else {
        cell = (CustomCell *)sender.superview.superview.superview;
    }

    //Get the cell's index path
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    //etc.

    NSLog(@"%@", cell.textField); //<---Crashes here

So, as you can see, I'm accounting for the extra view in iOS 7. After adding some breakpoints and taking a closer look at the variables, I see that cell exists, but all the subviews it has in the interface file (which are linked up) - including textField - are nil. At the line specified, I'm receiving the following crash log:

-[UITableViewWrapperView textField]: unrecognized selector sent to instance 0x12c651430

I looked into this further, and I found this:

Changing the else statement to be identical to the preceding line gets rid of the crash, and the app works fine (using sender.superview.superview like in iOS 6).

This makes no sense to me. Did Apple revert the hierarchy of UITableViewCells to that of iOS 6's, or am I missing something? Thanks!

like image 633
rebello95 Avatar asked Aug 15 '14 16:08

rebello95


2 Answers

I've encountered the same issue. Here's a more reliable method:

UITextField* textField = (UITextField*)sender;
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:[self.tableView convertPoint:textField.center fromView:textField.superview]];

This will work regardless of the underlying view hierarchy of UITableViewCell.

like image 54
geraldWilliam Avatar answered Sep 19 '22 17:09

geraldWilliam


Different iOs versions have different implementations of UITableViewor UITableViewController, as an easy fix you'll have to iterate through superviews until you found the desired view class instead of relying it being the N-th superview.

like image 45
A-Live Avatar answered Sep 17 '22 17:09

A-Live