您好:我一直在 iOS 6、7 和现在的 8(测试版 5)上测试我的应用程序。我的带有自定义 UITableViewCell
的 UITableView
在 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/