Hello: I've been testing my app on iOS 6, 7, and now 8 (beta 5). My UITableView
with custom UITableViewCell
s 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 UITableViewCell
s to that of iOS 6's, or am I missing something? Thanks!
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.
Different iOs versions have different implementations of UITableView
or 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With