tableview单元格中的Textfield在iOS7上崩溃

时间:2013-12-10 10:37:42

标签: iphone ipad uitableview ios7 uitextfield

我的代码在iOS 6上工作正常但在iOS 7中崩溃。

我将文本字段与表格视图单元格文本字段进行比较。

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{

    DLog(@"-> %@", textField.text);

    PhotoViewCustomCell *cell = (PhotoViewCustomCell*)[[textField superview] superview];
    NSIndexPath *indexPath = [tblPhotoDetail indexPathForCell:cell];

    //PhotoViewCustomCell *cell = (PhotoViewCustomCell *)[tblPhotoDetail cellForRowAtIndexPath:indexPath];

    PhotoInformation *objPhotoInfo = [selectedPhotos objectAtIndex:indexPath.row];

    if ([textField isEqual:cell.mytextfield]) 
    { =========>crashing in this line

       do something
    }
    else 
    {
      do something
    }
}

错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellScrollView mytextfield]: unrecognized selector sent to instance 0xdc119a0'

2 个答案:

答案 0 :(得分:4)

添加额外的超级视图调用以获取您的单元格。看起来你正在隐藏UITableViewCellScrollView,它位于contentView

之上的层次结构中
 PhotoViewCustomCell *cell = (PhotoViewCustomCell*)[[[textField superview] superview] superview];

答案 1 :(得分:2)

接受答案中的代码不是更安全的代码,也不能用于iOS 6。我宁愿建议你使用这段代码。 :

UIView *view = [textField superview];
while (![view isKindOfClass:[PhotoViewCustomCell class]]) {
    view = [view superview];
}
PhotoViewCustomCell *cell = (PhotoViewCustomCell *)view;