4

下面的代码在 IOS 5.0、6.0、6.1 等上返回“YES”,但在 IOS 7.0 上返回“NO”。你对此有什么想法吗?是 IOS 7.0 的错误吗?非常感谢..

[view isKindOfClass:[SimpleLabel class]]

PS:“SimpleLabel”是继承自UILabel的类。

- - 更新 - -

对不起,不清楚的问题。:(我在一个UITableViewCell类中使用上面的代码,并添加SimpleLabel如下;

 [self addSubview:label];

我覆盖layoutSubviews函数,循环self.subviews,但[view class]总是返回UITableViewCellScrollView

-(void)layoutSubviews {
[super layoutSubviews];
for (UIView*view in self.subviews) {
    if ([view isKindOfClass:[SimpleLabel class]]) {
        SimpleLabel*label = (SimpleLabel*)view;
4

1 回答 1

7

UITableViewCell的视图层次结构在 iOS 7 中略有变化

在 iOS <= 6 中,层次结构看起来像

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

而在 iOS 7 中就像

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

(来源:http ://www.curiousfind.com/blog/646 )

当您添加子视图时,它会插入UITableViewCellContentView比您要查找的位置更深的一层。

isKindOfClass:工作正常,问题是您正在通过错误的子视图集。

顺便说一句,这是一个很好的例子,说明为什么你永远不应该依赖内部视图层次结构:Apple 可以随时更改它们。

于 2013-10-18T06:03:42.513 回答