How to add a custom separator to UITableViewCell?

后端 未结 4 1171
北海茫月
北海茫月 2020-12-16 17:22

Please spare sometime as this is a long explanation

I have a UIViewController which consists of a UIButton and a UITableView w

4条回答
  •  一向
    一向 (楼主)
    2020-12-16 17:29

    I do it this way... hope this might help.

    //
    //  UITableViewCell+MyAdditions.h
    //
    //  Created by Roberto O. Buratti on 19/02/14.
    //
    
    #import 
    
    @interface UITableViewCell (MyAdditions)
    
    @property (nonatomic,assign) UITableViewCellSeparatorStyle cellSeparatorStyle;
    @property (nonatomic,strong) UIColor *cellSeparatorColor;
    
    @end
    
    //
    //  UITableViewCell+MyAdditions.m
    //
    //  Created by Roberto O. Buratti on 19/02/14.
    //
    
    #import "UITableViewCell+MyAdditions.h"
    
    NSString *const kUITablewViewCellSeparatorLayerName = @"kUITablewViewCellSeparatorLayerName";
    
    @implementation UITableViewCell (MyAdditions)
    
    -(CALayer *)separatorLayer
    {
        for (CALayer *sublayer in self.layer.sublayers)
        {
            if ([sublayer.name isEqualToString:kUITablewViewCellSeparatorLayerName])
                return sublayer;
        }
        return nil;
    }
    
    -(CALayer *)newSeparatorLayer
    {
        CALayer *separatorLayer = [CALayer layer];
        separatorLayer.name = kUITablewViewCellSeparatorLayerName;
        separatorLayer.frame = CGRectMake(0, self.bounds.size.height - 1, self.bounds.size.width, 1);
        separatorLayer.backgroundColor = [UIColor whiteColor].CGColor;
        [self.layer addSublayer:separatorLayer];
        return separatorLayer;
    }
    
    -(UITableViewCellSeparatorStyle)cellSeparatorStyle
    {
        CALayer *separatorLayer = [self separatorLayer];
        if (separatorLayer == nil)
            return UITableViewCellSeparatorStyleNone;
        else
            return UITableViewCellSeparatorStyleSingleLine;
    }
    
    -(void)setCellSeparatorStyle:(UITableViewCellSeparatorStyle)separatorStyle
    {
        CALayer *separatorLayer = [self separatorLayer];
        switch (separatorStyle)
        {
            case UITableViewCellSeparatorStyleNone:
                [separatorLayer removeFromSuperlayer];
                break;
            case UITableViewCellSeparatorStyleSingleLine:
                if (separatorLayer == nil)
                    separatorLayer = [self newSeparatorLayer];
                break;
            default:
                @throw [NSException exceptionWithName:NSStringFromClass([self class]) reason:@"Unsupported separatorStyle" userInfo:nil];
                break;
        }
    }
    
    -(UIColor *)cellSeparatorColor
    {
        CALayer *separatorLayer = [self separatorLayer];
        return [UIColor colorWithCGColor:separatorLayer.backgroundColor];
    }
    
    -(void)setCellSeparatorColor:(UIColor *)separatorColor
    {
        CALayer *separatorLayer = [self separatorLayer];
        if (separatorLayer == nil)
            separatorLayer = [self newSeparatorLayer];
        separatorLayer.backgroundColor = separatorColor.CGColor;
    }
    
    @end
    

    Now you can do things like

    UITableViewCell *cell = ...
    cell.cellSeparatorStyle = UITableViewCellSeparatorStyleSingleLine;
    cell.cellSeparatorColor = [UIColor orangeColor];
    

提交回复
热议问题