简体   繁体   English

iOS 8: - [UITableViewWrapperView textField]:发送到实例的无法识别的选择器

[英]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). 您好:我一直在iOS 6,7和现在8(测试版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. UITableView自定义UITableViewCell s的做工精细月6日和7然而,在iOS 8,我得到一个崩溃,当我试图访问一个子视图(文本字段)的细胞。

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: 我知道iOS 7中单元格层次结构中还有另一个视图。奇怪的是,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

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 . 所以,正如你所看到的,我正在考虑iOS 7中的额外视图。在添加一些断点并仔细查看变量后,我看到该cell存在,但它在接口文件中有所有子视图(其中已链接) - 包括textField - 是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). else语句更改为与前一行相同可以消除崩溃,并且应用程序正常工作(使用与iOS 6中类似的sender.superview.superview )。

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? Apple是否将UITableViewCell的层次结构恢复为iOS 6的层次结构,或者我错过了什么? 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. 无论UITableViewCell的底层视图层次结构如何,这都将起作用。

不同的iO版本具有不同的UITableViewUITableViewController ,作为一个简单的修复,您必须迭代superviews直到找到所需的视图类而不是依赖它作为第N个超级视图。

I also had the same issue on iOS8 with getting UITableViewCell via superview from a child view. 我在iOS8上也遇到了同样的问题,从子视图通过superview获取UITableViewCell。 This is what I came up with for both iOS7 and iOS8 support. 这就是我提出的iOS7和iOS8支持。

-(void)thumbTapped:(UITapGestureRecognizer*)recognizer {
    NSLog(@"Image inside a tableview cell is tapped.");

    UIImageView *mediaThumb = (UIImageView*)recognizer.view;
    UITableViewCell* cell;
    // get the index of container cell's row
    // bug with ios7 vs ios8 with superview level!! :(
    /*** For your situation the level of superview will depend on the design structure ***/
    // The rule of thumb is for ios7 it need an extra superview
    if (SYSTEM_VERSION_LESS_THAN(@"8.0")) { // iOS 7
        cell = (UITableViewCell*)mediaThumb.superview.superview.superview.superview;
    } else { // iOS 8
        cell = (UITableViewCell*)mediaThumb.superview.superview.superview;
    }
}

Just to clarify, in cellForRowAtIndexPath I assigned the tap gesture recognizer. 只是为了澄清,在cellForRowAtIndexPath中我分配了轻敲手势识别器。 The original design is very complex in Storyboard with many subviews, buttons.. etc. 故事板中的原始设计非常复杂,有许多子视图,按钮等。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MediaCell" forIndexPath:indexPath];
    .......    
    UIImageView *mediaImage = ............
    .....................................
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(thumbTapped:)];
    [mediaImage addGestureRecognizer:tapGestureRecognizer];

    return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM