无法访问 tableviewcell 中的 属性

Cannot access property in tableviewcell

我有一些自定义的 tableviewcells,每个都有一个带有手势识别器的缩略图,用于打开一个模式框。

在每个 tableviewcell 中都有一个 属性 包含一个字符串。

在手势识别器调用的方法中,我试图通过循环手势识别器的超级视图来访问此 属性。

 - (void)handleLongPress:(UILongPressGestureRecognizer *)LongPressGestureRecognizer
 {
    //handle press
    ZoomImageViewController *zoomImage = [self.storyboard instantiateViewControllerWithIdentifier:@"zoomImage"];

    UIView *subview = LongPressGestureRecognizer.view;
    while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
        subview = subview.superview;
    }

    UITableViewCell *cell = (UITableViewCell *)subview;

    zoomImage.filename = cell.machinePicture;
    zoomImage.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

    [self presentViewController:zoomImage animated:YES completion:nil];

}

在调试器中,单元格对象是包含我要访问的 属性 的 tableviewcellcontroller。 但是,在下一行我得到错误 "Property 'machinePicture' not found on object of type 'UITableViewCell *'"

zoomImage.filename = cell.machinePicture;

我不明白为什么找不到 属性,而单元格似乎是我正在寻找的正确对象,包含我想要的 属性...

标准UITableViewCell不包含machinePicture属性,您是否扩展了UITableViewCellclass?

改为使用该自定义 class,并进行相应的转换:

MyUITableViewCell *cell = (MyUITableViewCell *)subview;

UITableviewCell 你不会得到 属性 machinePicture 因为它不是 UITableViewCell 中的 属性。我认为您使用的是自定义 table 视图单元格。所以你必须对它进行类型转换。其次,您必须使用 UITableViewDelegatetableview:didSelectRowAtIndexPath: 方法来获取 tableviewcell,而不是使用手势识别器。