iOS 开发技巧iOS 开发学习

iOS-解决自定义cell.selectedBackground

2018-06-26  本文已影响2人  Andy_WangPeng

本文声明转载自CSDN->Mocha的文章

该哥们写的是很不错,就是有点乱。以下整理如下:

#import "BBMenuTableViewCell.h"

#define separatorViewTag 1000

@interface BBMenuTableViewCell ()
{
    UIView *customSeparatorView;
    CGFloat separatorHight;
}
@property (nonatomic,weak)UIView *originSeparatorView;
@end

@implementation BBMenuTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setSeparatorWithInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    [self setSeparatorColorWithColor:[UIColor colorWithRed:31/255.0 green:32/255.0f blue:35/255.0 alpha:0.2]];
}
/**
 
 * 解决选中cell selectedBackgroundView 显示时 分割线显示不全
 
 * @param insets insets description
 
 */
-(void)setSeparatorWithInset:(UIEdgeInsets)insets{
    if (customSeparatorView) {
        customSeparatorView.frame = CGRectMake(insets.left, insets.top,self.frame.size.width - insets.left - insets.right, self.originSeparatorView.frame.size.height-insets.bottom - insets.top);
        self.originSeparatorView.hidden = YES;
        self.originSeparatorView.alpha = 0;
    }else{
        for (UIView *subView in self.contentView.superview.subviews) {
            if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
                self.originSeparatorView = subView;
                subView.hidden = YES;
                subView.alpha = 0;
                subView.frame = CGRectMake(insets.left, insets.top,self.frame.size.width - insets.left - insets.right, subView.frame.size.height-insets.bottom - insets.top);
                customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
                if (!customSeparatorView) {
                    customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
                    customSeparatorView.tag = separatorViewTag;
                    [[subView superview] addSubview:customSeparatorView];
                    customSeparatorView.backgroundColor = [subView backgroundColor];
                }
                [[subView superview] bringSubviewToFront:customSeparatorView];
                break;
            }
        }
    }
}
-(void)setSeparatorColorWithColor:(UIColor *)sepColor{
    if (customSeparatorView) {
        customSeparatorView.backgroundColor = sepColor;
        self.originSeparatorView.hidden = YES;
        self.originSeparatorView.alpha = 0;
    }else {
        for (UIView *subView in self.contentView.superview.subviews) {
            if ([NSStringFromClass(subView.class) hasSuffix:@"SeparatorView"]) {
                self.originSeparatorView = subView;
                if (sepColor) {
                    subView.hidden = YES;
                    subView.alpha = 0;
                    subView.backgroundColor = sepColor;
                    customSeparatorView = [[subView superview] viewWithTag:separatorViewTag];
                    if (!customSeparatorView) {
                        customSeparatorView = [[UIView alloc] initWithFrame:subView.frame];
                        customSeparatorView.tag = separatorViewTag;
                        [[subView superview] addSubview:customSeparatorView];
                        customSeparatorView.backgroundColor = [subView backgroundColor];
                    }
                    [[subView superview] bringSubviewToFront:customSeparatorView];
                }
                break;
            }
        }
    }
}
@end
上一篇下一篇

猜你喜欢

热点阅读