美文网首页iOS 开发技巧iOS 开发学习
iOS-解决自定义cell.selectedBackground

iOS-解决自定义cell.selectedBackground

作者: Andy_WangPeng | 来源:发表于2018-06-26 15:44 被阅读2次

    本文声明转载自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
    

    相关文章

      网友评论

        本文标题:iOS-解决自定义cell.selectedBackground

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