Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isKindOfClass does not work as expected IOS 7

The code below, returns "YES" on IOS 5.0, 6.0, 6.1 etc. but returns "NO" on IOS 7.0. Dou you have an idea about that? Is it an IOS 7.0 bug? Thanks a lot..

[view isKindOfClass:[SimpleLabel class]]

PS: "SimpleLabel" is a class inherited from UILabel.

----UPDATE----

Sorry for unclear question. :( I use the code above in a UITableViewCell class, and add SimpleLabel as below;

 [self addSubview:label];

I override layoutSubviews function, loop in self.subviews, but [view class] always returns UITableViewCellScrollView.

-(void)layoutSubviews {
[super layoutSubviews];
for (UIView*view in self.subviews) {
    if ([view isKindOfClass:[SimpleLabel class]]) {
        SimpleLabel*label = (SimpleLabel*)view;
like image 723
seymatanoglu Avatar asked Mar 22 '23 15:03

seymatanoglu


1 Answers

UITableViewCell's view hierarchy changed slightly in iOS 7

In iOS <= 6 the hierarchy looks like

<UITableViewCell>
   | <UITableViewCellContentView>
   |    | <UILabel>

whereas in iOS 7 it's like

<UITableViewCell>
   | <UITableViewCellScrollView>
   |    | <UIButton>
   |    |    | <UIImageView>
   |    | <UITableViewCellContentView>
   |    |    | <UILabel>

(source: http://www.curiousfind.com/blog/646)

When you add a subview it is inserted in the UITableViewCellContentView which is one level deeper than where you are looking for.

isKindOfClass: works properly, the issues is that you are going through the wrong set of subviews.

By the way this is a splendid example of why you should never rely on internal view hierarchies: Apple can change them at any time.

like image 169
Gabriele Petronella Avatar answered Mar 31 '23 23:03

Gabriele Petronella