在iOS 7中使用超级视图获取UITableViewCell

我得到UITableViewCell UIButton属于这样:
-(void)buttonHandler:(UIButton *)button {

    OrderCell *cell = [[button superview] superview];
    NSLog(@"cell.item = %@",cell.item.text);

在iOS 7之前的任何事情都可以正常工作.但是给我:

[UITableViewCellScrollView item]:无法识别的选择器发送到实例0x17ae2cf0

如果我在iOS 7中运行应用程序,但如果我这样做:

-(void)buttonHandler:(UIButton *)button {

    OrderCell *cell = [[[button superview] superview] superview];
    NSLog(@"cell.item = %@",cell.item.text);

那么它可以在iOS 7中工作,但不能早期?

我通过这样做来规避这个问题:

OrderCell *cell;
if([[[UIDevice currentDevice] systemVersion] isEqualToString:@"7.0"])
    cell = [[[button superview] superview] superview];
else
    cell = [[button superview] superview];

NSLog(@"cell.item = %@",cell.item.text);

但是WTF正在进行中!
有人知道为什么会发生这种情况吗?

谢谢!

解决方法

更好的解决方案是为UIView(SuperView)添加一个类别,并通过以下方式调用它:
UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]

这样,您的代码适用于所有未来和过去的iOS版本

@interface UIView (SuperView)

- (UIView *)findSuperViewWithClass:(Class)superViewClass;

@end


@implementation UIView (SuperView)

- (UIView *)findSuperViewWithClass:(Class)superViewClass {

    UIView *superView = self.superview;
    UIView *foundSuperView = nil;

    while (nil != superView && nil == foundSuperView) {
        if ([superView isKindOfClass:superViewClass]) {
            foundSuperView = superView;
        } else {
            superView = superView.superview;
        }
    }
    return foundSuperView;
}
@end

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...