UITableView Group类型扁平化适配iOS6.0

适配方案

tableView

// 在ViewDidLoad里调用该方法
- (void)unifyTableView:(UITableView *)tableView {
    //iOS7以后将分隔线拉至边线
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
if ([self.tableView respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        self.tableView.preservesSuperviewLayoutMargins = NO;
}


    //ios7以前去除布质背景
    if ([[[UIDevice currentDevice]systemVersion]floatValue] < 7.0) {
        [tableView setBackgroundView:nil];
    }
}
// 实现UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     if (section == 0) {
          return 10.0;
     } else {
           return 0;
     }
}

tableViewCell

//系统xxxGroupCell基类中复写layoutSubViews
- (void) layoutSubviews {
    [super layoutSubviews];
    self.clipsToBounds = YES;
    // iOS7之后拉伸分格线至顶格
    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
            [self setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
        [self setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([[[UIDevice currentDevice]systemVersion]floatValue] < 7.0) {
        // 单元格颜色 iOS6下默认为浅灰, 更改为白色
        self.backgroundColor = [UIColor whiteColor];

        // group下contentView 左右各有缩进10, 调整至至cell同宽
        CGRect selfFrame = self.frame;
        UIView *contentView = self.contentView;
        [contentView setFrame:CGRectMake(0, contentView.frame.origin.y, selfFrame.size.width, contentView.frame.size.height)];

        // background frame
        CGFloat deviceWidth = self.superview.frame.size.width;
        CGFloat padding = 10.0;
        CGFloat cellHeight = self.frame.size.height;
        CGRect  frame = CGRectMake(-padding, 0, deviceWidth + 2*padding, cellHeight);
        self.backgroundView.frame = frame;
        self.selectedBackgroundView.frame = frame;

        // background imageView
        NSArray *views = [self subviews];
        for (UIView *subView in views) {
            if ([subView isKindOfClass:[UIImageView class]] ) {
                [(UIImageView *)subView setImage:nil];
                [(UIImageView *)subView setImage:[[UIImage alloc]init]];
            }
        }
    }
}

UITableView iOS6.0 解构

<UITableView: 0x203f7c00; 
    frame = (0 142; 320 426); 
    clipsToBounds = YES; 
    autoresize = RM+BM; 
    gestureRecognizers = <NSArray: 0x1fdb36c0>; 
    layer = <CALayer: 0x1fdb31a0>; 
    contentOffset: {0, 0}>

subviews

self.backgroundView

<UITableViewBackgroundView: 0x1fdb4400; 
    frame = (0 0; 320 426); 
    autoresize = W+H; 
    layer = <CALayer: 0x1fdb44b0>>

imageView * 2

<UIImageView: 0x1c569f40; frame = (0 419; 320 7); alpha = 0; opaque = NO; autoresize = TM; userInteractionEnabled = NO; layer = <CALayer: 0x1c569fe0>>,
<UIImageView: 0x1c56a0a0; frame = (313 386; 7 40); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; layer = <CALayer: 0x1c56a140>>
)

两个imageView的image图片均为 用于展示横向或竖向的滑块

UITableViewCell iOS6.0 解构

<NEPGoupCell: 0x1dde01f0; 
    baseClass = UITableViewCell;
    frame = (0 10; 480 46); 
    text = '参数配置(日志查看)'; 
    autoresize = W; 
    animations = { position=<CABasicAnimation: 0x1dda5240>;
    bounds=<CABasicAnimation: 0x1dda52a0>; }; 
    layer = <CALayer: 0x1dde0370>>

Subviews

self.contentView

<UITableViewCellContentView: 0x1dde0630; 
    frame = (0 1; 480 43); 
    gestureRecognizers = <NSArray: 0x1dde0b90>; 
    animations = { position=<CABasicAnimation: 0x1ddc3ea0>; 
    bounds=<CABasicAnimation: 0x1ddd6070>; }; 
    layer = <CALayer: 0x1dde0820>>

self.backgroundView

<UIGroupTableViewCellBackground: 0x1dde1d00; 
    frame = (9 0; 462 46); 
    opaque = NO; autoresize = W; 
    animations = { position=<CABasicAnimation: 0x1ddc3e50>; 
    bounds=<CABasicAnimation: 0x1ddc3eb0>; }; 
    layer = <CALayer: 0x1dde1bb0>>,

self.selectedBackgroundView

<UIGroupTableViewCellBackground: 0x1cdc4260; 
frame = (9 0; 302 45);
 layer = <CALayer: 0x1cdbfaa0>>

在cell创建时即被创建,由self.selectedBackgroundView强持有,当tableview: didSelected时,系统执行[cell addSubview:self.selectedBackgroundView]

backgroundImageView

只存在于setion x row 0 ,图片为

Layers

<CALayer:0x1cde3da0; 
position = CGPoint (160 88.6);
bounds = CGRect (0 0; 320 45); 
delegate = <NEPGoupCell: 0x1cde3c70; 
    baseClass = UITableViewCell; 
    frame = (0 66.1; 320 45); 
    text = '支付'; 
    autoresize = W; 
    layer = <CALayer: 0x1cde3da0>>;

 sublayers = (<CALayer: 0x1cde55c0>,
                <CALayer: 0x1cdbfaa0>, 
                <CALayer: 0x1cde3e30>, 
                <CALayer: 0x1cde6570>); 

 backgroundColor = <CGColor 0x1cdbec60> 
    [<CGColorSpace 0x1cd913b0> (kCGColorSpaceDeviceGray)] ( 0 0 )>

subLayer 0x1cde55c0

<CALayer:0x1cde55c0; position = CGPoint (160 22.5); 
bounds = CGRect (0 0; 302 45); 
delegate = <UIGroupTableViewCellBackground: 0x1cde5600; 
    frame = (9 0; 302 45); 
    alpha = 0; 
    autoresize = W; 
    layer = <CALayer: 0x1cde55c0>>; 

contents = <CGImage 0x1cdeec60>; 
sublayers = (<CALayer: 0x1cde5320>); 
opaque = YES; 
opacity = 0; 
rasterizationScale = 2; 
contentsScale = 2>

subLayer 0x1cdbfaa0

<CALayer:0x1cdbfaa0; 
position = CGPoint (160 22.5); 
bounds = CGRect (0 0; 302 45);
delegate = <UIGroupTableViewCellBackground: 0x1cdc4260; 
    frame = (9 0; 302 45); 
    layer = <CALayer: 0x1cdbfaa0>>; 

contents = <CGImage 0x1cdc6450>; 
sublayers = (<CALayer: 0x1cdc3320>); 
opaque = YES; 
rasterizationScale = 2; 
contentsScale = 2>

subLayer 0x1cde3e30

<CALayer:0x1cde3e30; 
position = CGPoint (160 22.5); 
bounds = CGRect (0 0; 320 43); 
delegate = <UITableViewCellContentView: 0x1cde3dd0; 
    frame = (0 1; 320 43); 
    opaque = NO; 
    gestureRecognizers = <NSArray: 0x1cde4050>; 
    layer = <CALayer: 0x1cde3e30>>; 

sublayers = (<CALayer: 0x1cde5180>); 
backgroundColor = <CGColor 0x1cdbec60> 
    [<CGColorSpace 0x1cd913b0> (kCGColorSpaceDeviceGray)] ( 0 0 )>

subLayer 0x1cde6570

<CALayer:0x1cde6570; 
position = CGPoint (160 4.75); 
bounds = CGRect (0 0; 300 7.5); 
delegate = <UIImageView: 0x1cde5cc0; 
    frame = (10 1; 300 7.5); 
    opaque = NO; 
    userInteractionEnabled = NO; 
    layer = <CALayer: 0x1cde6570>>; 

backgroundColor = <CGColor 0x1cdbec60> [<CGColorSpace 0x1cd913b0> 
    (kCGColorSpaceDeviceGray)] ( 0 0 ); 
opacity = 1; rasterizationScale = 1; 
contentsScale = 1; 
contentsCenter = CGRect (0.3745 0.998667; 0.001 0.00266667)>

你可能感兴趣的:(UITableView Group类型扁平化适配iOS6.0)