iOS7对UITableViewCell的层级改变

来源:互联网 发布:非农数据修正时间 编辑:程序博客网 时间:2024/05/29 03:57
【现象】:
在iOS6下将某个myView插入到cell的contentView底下,在iOS7上却没有效果。
即在iOS7下调用:

    [cell insertSubview:myView belowSubview:cell.contentView];

方法后,myView却仍然在contentView的上面。

【研究】:
(实验1)insertSubview:belowSubview:研究
执行如下代码:

    [whiteColorView addSubview:redColorView];

    [whiteColorView addSubview:greenColorView];

    [whiteColorView insertSubview:blueColorView belowSubview:greenColorView];

显示层级从下至上为:white—>red—>blue—>green

执行如下代码:

    [whiteColorView addSubview:redColorView];

    [redColorView addSubview:greenColorView];

    [whiteColorView insertSubview:blueColorView belowSubview:greenColorView];

显示层级从下至上为:white—>red—>green—>blue

以上两段代码在iOS6和iOS7上执行的效果相同。

(实验2)UITableViewCell的层级研究

tableView:cellForRowAtIndexPath:方法中执行如下代码:

    NSLog(@"%@", cell);

    NSLog(@"%@", cell.superview);

    NSLog(@"%@", cell.contentView);

    NSLog(@"%@", cell.contentView.superview);

    NSLog(@"%@", cell.contentView.superview.superview);


在iOS6上的输出结果为:

2014-01-17 02:34:22.917 InsertViewDemo[76500:907] <Cell: 0x754d680; baseClass = UITableViewCell; frame = (0 0; 320 100); autoresize = RM+BM; layer = <CALayer: 0x754d800>>

2014-01-17 02:34:22.919 InsertViewDemo[76500:907] (null)

2014-01-17 02:34:22.920 InsertViewDemo[76500:907] <UITableViewCellContentView: 0x754d950; frame = (0 0; 320 100); clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x754e9b0>; layer = <CALayer: 0x754d9e0>>

2014-01-17 02:34:22.921 InsertViewDemo[76500:907] <Cell: 0x754d680; baseClass = UITableViewCell; frame = (0 0; 320 100); autoresize = RM+BM; layer = <CALayer: 0x754d800>>

2014-01-17 02:34:22.922 InsertViewDemo[76500:907] (null)

在iOS7上的输出结果为:

2014-01-17 02:26:20.561 InsertViewDemo[76452:70b] <Cell: 0x8a10710; baseClass = UITableViewCell; frame = (0 0; 320 100); autoresize = RM+BM; layer = <CALayer: 0x8a10900>>

2014-01-17 02:26:20.562 InsertViewDemo[76452:70b] (null)

2014-01-17 02:26:20.563 InsertViewDemo[76452:70b] <UITableViewCellContentView: 0x8a10af0; frame = (0 0; 320 100); clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x8a12270>; layer = <CALayer: 0x8a10ba0>>

2014-01-17 02:26:20.563 InsertViewDemo[76452:70b] <UITableViewCellScrollView: 0x8a113a0; frame = (0 0; 320 100); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x8a11950>; layer = <CALayer: 0x8a115d0>; contentOffset: {0, 0}>

2014-01-17 02:26:20.564 InsertViewDemo[76452:70b] <Cell: 0x8a10710; baseClass = UITableViewCell; frame = (0 0; 320 100); autoresize = RM+BM; layer = <CALayer: 0x8a10900>>
真相大白了,在iOS7下,cell与cell.contentView之间多了一层:UITableViewCellScrollView。

分析】:
1、通过实验1可以看出,在iOS6和iOS7上insertSubview:belowSubview:方法的执行效果相同。UIView只能管理自己的直接子视图层级,即belowSubview:后面的参数必须是直接子视图,否则该方法的效果等同于addSubview:方法。
2、通过实验2可以看出,在iOS6上UITableViewCell的层级为:
UITableViewCell—>UITableViewCellContentView;
在iOS7上UITableViewCell的层级为:
UITableViewCell—>UITableViewCellScrollView—>UITableCellContentView。
3、iOS7上多了一层,cell不再是contentView的superview,于是

    [cell insertSubview:myView belowSubview:cell.contentView];

这个方法执行失败,相当于只是[cell addSubview:myView];,所以达不到想要的效果。

【解决方案】:
要在iOS6和iOS7上都能实现将myView插入到contentView底下的效果,将语句改成如下方式即可:

    [cell.contentView.superview insertSubview:myView belowSubview:cell.contentView];


【吐槽】:
坑爹的苹果,坑爹的iOS7,我暂时没有找到任何文档有关于UITableViewCellScrollView这个类的描述,也无法直接引用这个类,Xcode会提示”Unknown type name 'UITableViewCellScrollView’”。还好可以用上面的方法间接引用,否则完全是作死啊。
0 0
原创粉丝点击