【问题标题】:Textfield in tableview cell crashes on iOS7iOS7上tableview单元格中的文本字段崩溃
【发布时间】:2013-12-10 10:37:41
【问题描述】:

我的代码在 iOS6 上运行良好,但在 iOS7 中崩溃。

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

- (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'

【问题讨论】:

    标签: iphone ipad uitableview ios7 uitextfield


    【解决方案1】:

    添加额外的 superview 调用以获取您的单元格。看起来你正在隐藏 UITableViewCellScrollView,它在 contentView 上方的层次结构中

     PhotoViewCustomCell *cell = (PhotoViewCustomCell*)[[[textField superview] superview] superview];
    

    【讨论】:

      【解决方案2】:

      Accepted Answer 中的代码不是更安全的代码,也不能用于iOS 6。我宁愿建议您使用此代码。 :

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-23
        • 1970-01-01
        • 2013-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多