美文网首页iOS大咖说
设置不同section中cell的圆角

设置不同section中cell的圆角

作者: YannChee | 来源:发表于2017-02-06 17:39 被阅读228次

    项目中经常要用到标题为我的或者设置等的界面,而这些界面一般用storyboard搭建static的cell比较方便快捷,我们经常会遇到以下需求:

    • 1.tabbleview在group样式下,自定义不同section的间距
    • 2.为不同section中的cell设置圆角
      我的解决办法如下:

    1.设置section的间距

    • 调用代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section ;设置section的header高度 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section设置section的Footer高度
    • 或者调用代理方法 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section ;设置section的header高度;然后直接设置 sectionFooterHeight属性值;
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 10;
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.sectionFooterHeight = 8;
    }
    

    注意 : 带有导航控制器的导航栏时,直接设置sectionHeaderHeight属性值,self.tableView.sectionHeaderHeight=10;self.tableView.sectionFooterHeight = 8;会导致第一个section与导航条的间距和其他间距不同,所以最好通过代理方法- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;来设置sectionHeaderHeight的高度

    这种情况下,如果不用代理方法,需要设置tableView的contentInset属性,来解决

    - (void)viewDidLoad {
        [super viewDidLoad];
        
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
        self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
        self.tableView.sectionFooterHeight = 8;
        self.tableView.sectionHeaderHeight = 10;
    }
    

    2.设置不同section之间cell的圆角

    在- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;这个代理方法中作判断,在相应的cell中的图层绘制

    以下代码可直接复制粘贴使用

    - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
        
            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:1.0f].CGColor; // cell填充颜色
            layer.strokeColor=[UIColor whiteColor].CGColor; // cell边框颜色
            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 *cellBackView = [[UIView alloc] initWithFrame:bounds];
            [cellBackView.layer insertSublayer:layer atIndex:0];
            cellBackView.backgroundColor = UIColor.clearColor;
            cell.backgroundView = cellBackView;
        
    }
    

    运行效果如下:

    上图中第三个section中的cell是单独设置的,因为上面的代理方法中把cell的填充色重新绘制成了白色
    所以直接改变cell的背景色会不起作用(如图黄色部分),修改cell的contentView的背景色(如图绿色部分)又会把圆角给遮挡住

    运行效果

    所以我对第三个section的cell作了重新设置,自定义了名为QYMineCell的cell,重写- (void)setFrame:(CGRect)frame ;方法和设置圆角半径,并与storyboard对应的cell作关联

    #import "QYMineCell.h"
    
    @implementation QYMineCell
    
    - (void)awakeFromNib {
        [super awakeFromNib];
        // Initialization code
     
        self.layer.cornerRadius = 5.f;
    }
    
    - (void)setFrame:(CGRect)frame {
        CGFloat cellMargin = 10;
        frame.origin.x = cellMargin;
        frame.size.width -= cellMargin * 2;
        
        [super setFrame:frame];
    }
    
    @end
    

    相关文章

      网友评论

        本文标题:设置不同section中cell的圆角

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