Getting UITableViewCell with superview in iOS 7

前端 未结 5 1986
栀梦
栀梦 2021-01-04 20:18

I\'m getting the UITableViewCell a UIButton belongs to like this:

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

    OrderCell *cell = [[button superview] superv         


        
5条回答
  •  生来不讨喜
    2021-01-04 21:10

    A better solution is to add a category for UIView(SuperView), and calling it by:

    UITableViewCell *cell = [button findSuperViewWithClass:[UITableViewCell class]]
    

    This way, your code works for all future and past iOS versions

    @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
    

提交回复
热议问题