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

标签 ios objective-c uitableview ios8

您好:我一直在 iOS 6、7 和现在的 8(测试版 5)上测试我的应用程序。我的带有自定义 UITableViewCellUITableView 在 6 和 7 上运行良好。但是,在 iOS 8 上,当我尝试访问 subview (文本字段)时出现崩溃) 的细胞。

我知道在 iOS 7 的单元格层次结构中有另一个 View 。奇怪的是,在 iOS 8 中似乎不是这种情况。这是我使用的代码:

    //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

因此,如您所见,我考虑了 iOS 7 中的额外 View 。添加一些断点并仔细查看变量后,我看到 cell 存在,但所有它在界面文件(链接起来)中的 subview - 包括 textField - 是 nil。在指定的行,我收到以下崩溃日志:

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

我进一步研究了这个,我发现了这个:

else 语句更改为与前一行相同的语句可以避免崩溃,应用程序运行正常(使用 sender.superview.superview 就像在 iOS 6 中一样)。

这对我来说毫无意义。 Apple 是否将 UITableViewCell 的层次结构恢复为 iOS 6 的层次结构,还是我遗漏了什么?谢谢!

最佳答案

我遇到了同样的问题。这是一个更可靠的方法:

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

无论 UITableViewCell 的底层 View 层次如何,这都会起作用。

关于iOS 8 : -[UITableViewWrapperView textField]: unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25329891/

相关文章:

ios - Apple Watch 模拟器以通知 View 启动 watch 应用程序。如何在main中自动启动?

ios - 停止从 ScrollView 内的字段自动缩放

iphone - 消息发送到应该存在的对象上的已释放实例

objective-c - UIAppearance 对 Label 文本颜色没有影响

ios - UItextfield shouldChangeCharactersInRange方法BackSpace不起作用

objective-c - ios:通过仅进行一次替换来替换文件中多个字符串的最佳方法是什么?

ios - 询问一个似乎有空条目的 NSArray

ios - 如何在没有任何质量损失的情况下将图像设置到 UIImageView 中并获得图像的平均颜色

ios - 每次返回单元格时增加一个值

ios - 带有自定义 UIEdgeInsets 的 UILabel 在 UITableViewCell 中截断