简体   繁体   中英

Can't see dynamic subview on iOS7(Working on iOS6)

I have a custom TableViewCell. In the cell, I add two cross icons (using unicode) to both sides of the cell. when the user pans the cell, it will display the cross icon on the side.

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // add a cross
        _crossLabel = [self createCueLabel];
        _crossLabel.text = @"\u274C";
        _crossLabel.textAlignment = NSTextAlignmentLeft;
        // none of the following code works
        [self insertSubview:_crossLabel aboveSubview:self];
        [self insertSubview:_crossLabel belowSubview:self];
        [self addSubview:_crossLabel];

        _crossLabel2 = [self createCueLabel];
        _crossLabel2.text = @"\u274C";
        _crossLabel2.textAlignment = NSTextAlignmentLeft;
        [self addSubview:_crossLabel2];

        // add a pan recognizer
        UIGestureRecognizer* recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        recognizer.delegate = self;
        [self addGestureRecognizer:recognizer];
    }
    return self;
}

I used the code above to achieve that. And the _crossLabel did add to the Custom TableView Cell.

I used Reveal App to check the layout of my iOS app 在此输入图像描述 I can see _crossLabel has been added to my Cell. But I can't see the cross icon in my iOS 7 simulator. I have tried different methods to add the subView, but none of them works.

在此输入图像描述

But it works perfectly on iOS6 and the layout is exactly same as iOS 7 when I check in Reveal App.

Thanks for your help.

Make sure you are adding to the cell's contentView, [self.contentView addSubView:_crossLabel2]; and not the cell itself. You will see when using Reveal and inspecting iOS7, that in a UITableViewCell UIKit has added/slipped in a UITableViewCellSCrollView above the cell view, so be careful with your insertSubview:belowSubview calls. Also from your screenshot of the OutlineView of Reveal, the 'LocationCell' view is greyed out, which means it is hidden.

Edit just for future reference:
In iOS 7 the new UITableViewCellScrollView has it's 'clipToBounds' property set. It's a hack but if you [self.contentView.superview setClipsToBounds:NO] . The superview is the UITableViewCellScrollView on iOS7 and the cell itself on iOS6

No need to add on cells contentview. Simply you can access subview in iOS7 using

[[cell.subviews lastObject] subviews]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM