如何实现iOS6的TableView分组样式

作者: yzhxcql | 来源:发表于2016-05-24 14:07 被阅读794次

    iOS7之后Apple大大极力推行扁平化。各种App的风格从立体的变成平面,形态上从丰富变得简约。

    • iOS6下的TableView是这样子的
    带有明显圆角风格
    • 而iOS7下的TableView是这样子的
    又扁又平棱角分明
    • 可是问题来了。如何在新版的iOS系统上实现旧版的样式呢?

    找一找系统有没有保留旧的样式接口?骚年,这条路不行。
    目前实现的方法就是自己手写样式,在cell上动手脚。具体怎么实现的,请往下看。

    //在tableView即将显示cell的时候改动cell的样式,通常cell展示动画也是在这里做的。
    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        
        if ([cell respondsToSelector:@selector(tintColor)]) {
            
            if (tableView == self.tableView) {
                
                CGFloat cornerRadius = 5.f;
                
                cell.backgroundColor = UIColor.clearColor;
                
                CAShapeLayer *layer = [[CAShapeLayer alloc] init];
                
                CGMutablePathRef pathRef = CGPathCreateMutable();
                
                CGRect bounds = CGRectInset(cell.bounds, 10, 0);
                
                BOOL addLine = NO;
                
                if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                    
                    CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
                    
                } else if (indexPath.row == 0) {
                    
                    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
                    
                    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
                    
                    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                    
                    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
                    
                    addLine = YES;
                    
                } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                    
                    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
                    
                    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
                    
                    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                    
                    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
                    
                } else {
                    
                    CGPathAddRect(pathRef, nil, bounds);
                    
                    addLine = YES;
                    
                }
                
                layer.path = pathRef;
                
                CFRelease(pathRef);
                
                layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
    
                if (addLine == YES) {
                    
                    CALayer *lineLayer = [[CALayer alloc] init];
                    
                    CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
                    
                    lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
                    
                    lineLayer.backgroundColor = tableView.separatorColor.CGColor;
                    
                    [layer addSublayer:lineLayer];
                    
                }
                
                UIView *testView = [[UIView alloc] initWithFrame:bounds];
                
                [testView.layer insertSublayer:layer atIndex:0];
                
                testView.backgroundColor = UIColor.clearColor;
                
                cell.backgroundView = testView;
                
            }
        }
    }
    
    • 代码就这多,具体实现多看看就懂了。

    全文完。

    相关文章

      网友评论

      • feng_dev:就是 plain和Group的 样式 的 区别
        feng_dev:@Adoma 恩恩 ,感谢大神。
        yzhxcql:@枫子哥 plain的sectionHeaderView可以悬浮,group的不行。
      • feng_dev:想知道 tableview 的 特别 具体的 区别 ,纠结啊
      • 小凡凡520:在新版的实现旧版。我没有看错吧
        小凡凡520:@Adoma 产品经理去吃诗
        yzhxcql:@小凡凡520 有的设计图是这个需求..

      本文标题:如何实现iOS6的TableView分组样式

      本文链接:https://www.haomeiwen.com/subject/wfszrttx.html