美文网首页
iOS tableView无数据 占位图封装

iOS tableView无数据 占位图封装

作者: PANZHI蜕变 | 来源:发表于2020-02-28 23:23 被阅读0次

    适用于各种无数据view显示

    SYLoadingResultView.h
    #import <UIKit/UIKit.h>
    
    typedef NS_ENUM(NSUInteger, SYLoadingResultStyle) {
        SYLoadingResultDefault, //两个标题一个按钮(默认)
        SYLoadingResultImageTitleBtn, //一张图片、一个标题、一个按钮
        SYLoadingResultImageTitle, //一张图片、一个标题。
        SYLoadingResultImageTwoTitle, //一张图片、两个标题
        SYLoadingResultImageTwoTitleBtn, //一张图片、两个标题、一个按钮
    };
    
    @protocol SYLoadingResultViewDeleagte <NSObject>
    
    @optional
    - (void)loadingResultViewBtnClicked:(UIButton *)sender;
    
    @end
    
    typedef void(^callBackBlock)(UIButton *btn);
    
    @interface SYLoadingResultView : UIView
    
    @property (nonatomic, weak) id <SYLoadingResultViewDeleagte> delegate;
    
    + (SYLoadingResultView *)showLoadingResultOnView:(UIView *)view withBlock:(callBackBlock)block;
    
    + (SYLoadingResultView *)showLoadingResultOnView:(UIView *)View withStyle:(SYLoadingResultStyle)style withBlock:(callBackBlock)block;
    
    
    + (SYLoadingResultView *)showNotDataWithOnView:(UIView *)view;
    + (SYLoadingResultView *)showLoadingResultView:(UIView *)view reloadBlock:(callBackBlock)block;
    
    + (void)dismissOnView:(UIView *)view;
    
    - (void)setUpCallBackBlock:(callBackBlock)block;
    
    - (void)setMainTitleStr:(NSString *)string;
    - (void)setMainTitleAttriStr:(NSAttributedString *)attriStr;
    
    - (void)setSubTitleStr:(NSString *)string;
    - (void)setSubTitleAttriStr:(NSAttributedString *)attriStr;
    
    - (void)setImg:(UIImage *)image;
    
    - (void)setBtnStr:(NSString *)BtnStr;
    
    - (void)setContentViewBackgroundColor:(UIColor *)backgroundColor;
    
    - (void)setContainerViewCenterY:(CGFloat)centerY;
    
    @end
    
    
    
    
    
    SYLoadingResultView.m
    
    #import "SYLoadingResultView.h"
    #import "Masonry.h"
    
    //static NSString *const defaultMainLabelStr = @"数据加载失败";
    //static NSString *const defaultSubLabelStr = @"请检查您的手机是否联网,点击按钮重新加载";
    //static NSString *const defaultBtnStr = @"重新加载";
    
    
    static NSString * defaultMainLabelStr = @"数据加载失败";
    static NSString * defaultSubLabelStr = @"请检查您的手机是否联网,点击按钮重新加载";
    static NSString * defaultBtnStr = @"重新加载";
    
    
    
    @interface SYLoadingResultView ()
    
    @property (nonatomic, assign) SYLoadingResultStyle style;
    @property (nonatomic, assign) CGFloat containerCenterY;
    @property (nonatomic, strong) UIView *backgroundView;
    @property (nonatomic, strong) UIView *containerView;
    @property (nonatomic, strong) UIImageView *imageView;
    @property (nonatomic, strong) UILabel *mainLabel;
    @property (nonatomic, strong) UILabel *subLabel;
    @property (nonatomic, strong) UIButton *button;
    @property (nonatomic, copy) callBackBlock clickBlock;
    
    @end
    
    @implementation SYLoadingResultView
    
    #pragma mark - LifeCycle
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [self initWithStyle:SYLoadingResultDefault withFrame:frame];
        return self;
    }
    
    - (instancetype)initWithStyle:(SYLoadingResultStyle)style
    {
        self = [self initWithStyle:style withFrame:CGRectZero];
        return self;
    }
    
    - (instancetype)initWithStyle:(SYLoadingResultStyle)style withFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            defaultMainLabelStr = JDLocalized(@"errorPage_title", nil);
            defaultSubLabelStr  = JDLocalized(@"errorPage_tip", nil);
            defaultBtnStr       = JDLocalized(@"errorPage_btnText", nil);
            
            _style = style;
            _containerCenterY = 0;
            
            _backgroundView = [UIView new];
            _backgroundView.backgroundColor = JDColor(0xf5f5f5);
            
            _containerView = [UIView new];
            _containerView.backgroundColor = JDColor(0xf5f5f5);
            
            _imageView = [UIImageView new];
            _imageView.image = [UIImage imageNamed:@"No-net"];
            
            _mainLabel = [UILabel new];
            _mainLabel.text = defaultMainLabelStr;
            _mainLabel.font = [UIFont systemFontOfSize:17];
            _mainLabel.textColor = JDColor(0x333333);
            _mainLabel.textAlignment = NSTextAlignmentCenter;
            _mainLabel.numberOfLines = 0;
            switch (style) {
                case SYLoadingResultDefault:
                    _mainLabel.text = defaultMainLabelStr;
                    break;
                case SYLoadingResultImageTitleBtn:
                    _mainLabel.text = defaultMainLabelStr;
                    break;
                case SYLoadingResultImageTitle:
                    _mainLabel.text = defaultMainLabelStr;
                    break;
                case SYLoadingResultImageTwoTitle:
                    _mainLabel.text = defaultMainLabelStr;
                    break;
                default:
                    break;
            }
            
            _subLabel = [UILabel new];
            _subLabel.text = defaultSubLabelStr;
            _subLabel.font = [UIFont systemFontOfSize:13];
            _subLabel.textColor = JDColor(0x999999);
            _subLabel.textAlignment = NSTextAlignmentCenter;
            _subLabel.numberOfLines = 0;
            
            _button = [UIButton buttonWithType:UIButtonTypeCustom];
            [_button setTitle:defaultBtnStr forState:UIControlStateNormal];
            [_button setTitleColor:JDColor(0xe6454a) forState:UIControlStateNormal];
            [_button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
            UIImage *reloadBtnImg = [UIImage imageNamed:@"Roundbutton"];
            reloadBtnImg = [reloadBtnImg resizableImageWithCapInsets:UIEdgeInsetsMake(2, 19, 2, 19)
                                                        resizingMode:UIImageResizingModeStretch];
            [_button setBackgroundImage:reloadBtnImg forState:UIControlStateNormal];
            UIImage *reloadSelBtnImg = [UIImage imageNamed:@"RoundbuttonSelected"];
            reloadSelBtnImg = [reloadSelBtnImg resizableImageWithCapInsets:UIEdgeInsetsMake(2, 19, 2, 19)
                                                              resizingMode:UIImageResizingModeStretch];
            [_button setBackgroundImage:reloadSelBtnImg forState:UIControlStateHighlighted];
            [_button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
        }
        return self;
    }
    
    - (void)updateConstraints
    {
        [self addSubview:self.backgroundView];
        [self.backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        
        [self updateConstraintsWithStyle:self.style];
        
        [self addSubview:self.containerView];
        [self.containerView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.centerY.mas_equalTo(self.containerCenterY);
        }];
        
        [super updateConstraints];
    }
    
    - (void)updateConstraintsWithStyle:(SYLoadingResultStyle)style
    {
        [self.containerView removeAllSubviews];
        switch (style) {
            case SYLoadingResultDefault:
            {
                [self.containerView addSubview:self.mainLabel];
                [self.mainLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.left.right.mas_equalTo(0);
                }];
                
                [self.containerView addSubview:self.subLabel];
                [self.subLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.mainLabel.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                }];
                
                [self.containerView addSubview:self.button];
                [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.subLabel.mas_bottom).mas_offset(20);
                    make.centerX.mas_equalTo(0);
                    make.size.mas_equalTo(CGSizeMake(150, 40));
                    make.bottom.mas_equalTo(0);
                }];
            }
                break;
            case SYLoadingResultImageTitleBtn:
            {
                [self.containerView addSubview:self.imageView];
                [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_offset(0);
                    make.centerX.mas_equalTo(self.containerView.mas_centerX);
                    make.size.mas_equalTo(self.imageView.image.size);
                }];
                
                [self.containerView addSubview:self.mainLabel];
                [self.mainLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                }];
                
                [self.containerView addSubview:self.button];
                [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.mainLabel.mas_bottom).mas_offset(20);
                    make.size.mas_equalTo(CGSizeMake(150, 40));
                    make.centerX.mas_equalTo(self.containerView.mas_centerX);
                    make.bottom.mas_equalTo(0);
                }];
            }
                break;
            case SYLoadingResultImageTitle:
            {
                [self.containerView addSubview:self.imageView];
                [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_offset(0);
                    make.centerX.mas_equalTo(self.containerView.mas_centerX);
                    make.size.mas_equalTo(self.imageView.image.size);
                }];
                
                [self.containerView addSubview:self.mainLabel];
                [self.mainLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                    make.bottom.mas_equalTo(0);
                }];
            }
                break;
            case SYLoadingResultImageTwoTitle:
            {
                [self.containerView addSubview:self.imageView];
                [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_offset(0);
                    make.centerX.mas_equalTo(self.containerView.mas_centerX);
                    make.size.mas_equalTo(self.imageView.image.size);
                }];
                
                [self.containerView addSubview:self.mainLabel];
                [self.mainLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                }];
                
                [self.containerView addSubview:self.subLabel];
                [self.subLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.mainLabel.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                    make.bottom.mas_equalTo(0);
                }];
            }
                break;
            case SYLoadingResultImageTwoTitleBtn:
            {
                [self.containerView addSubview:self.imageView];
                [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_offset(0);
                    make.centerX.mas_equalTo(self.containerView.mas_centerX);
                    make.size.mas_equalTo(self.imageView.image.size);
                }];
                
                [self.containerView addSubview:self.mainLabel];
                [self.mainLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.imageView.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                }];
    
                [self.containerView addSubview:self.subLabel];
                [self.subLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.mainLabel.mas_bottom).mas_offset(10);
                    make.left.right.mas_equalTo(0);
                }];
    
                [self.containerView addSubview:self.button];
                [self.button mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(self.subLabel.mas_bottom).mas_offset(20);
                    make.size.mas_equalTo(CGSizeMake(150, 40));
                    make.centerX.mas_equalTo(self.containerView.mas_centerX);
                    make.bottom.mas_equalTo(0);
                }];
            }
                break;
            default:
                break;
        }
    }
    
    + (SYLoadingResultView *)showLoadingResultOnView:(UIView *)view withBlock:(callBackBlock)block
    {
        return [SYLoadingResultView showLoadingResultOnView:view withStyle:SYLoadingResultDefault withBlock:block];
    }
    
    + (SYLoadingResultView *)showLoadingResultOnView:(UIView *)View withStyle:(SYLoadingResultStyle)style withBlock:(callBackBlock)block
    {
        if (!View) {
            return nil;
        }
        SYLoadingResultView *loadingResultView = [[SYLoadingResultView alloc] initWithStyle:style];
        [View addSubview:loadingResultView];
        [loadingResultView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        
        [loadingResultView setUpCallBackBlock:block];
        
        return loadingResultView;
    }
    
    + (SYLoadingResultView *)showNotDataWithOnView:(UIView *)view
    {
        SYLoadingResultView *resultView = [SYLoadingResultView showLoadingResultOnView:view withStyle:SYLoadingResultImageTitle withBlock:nil];
        
        UIImage *img = [UIImage imageNamed:@"error_notdata"];
        [resultView setImg:img];
        
        NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:@"没有数据记录"];
        [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0, attriStr.length)];
        //添加文字背景颜色
        [attriStr addAttribute:NSForegroundColorAttributeName value:JDColor(0x999999) range:NSMakeRange(0, attriStr.length)];
        [resultView setMainTitleAttriStr:attriStr];
        
        [resultView updateConstraintsIfNeeded];
        return resultView;
    }
    
    + (SYLoadingResultView *)showLoadingResultView:(UIView *)view reloadBlock:(callBackBlock)block
    {
        
        SYLoadingResultView *resultView = [SYLoadingResultView showLoadingResultOnView:view withStyle:SYLoadingResultImageTitleBtn withBlock:block];
        
        UIImage *img = [UIImage imageNamed:@"error_noNet"];
        [resultView setImg:img];
        
        NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:@"亲, 您的网络不给力哦!"];
        [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14.0] range:NSMakeRange(0, attriStr.length)];
        //添加文字背景颜色
        [attriStr addAttribute:NSForegroundColorAttributeName value:JDColor(0x999999) range:NSMakeRange(0, attriStr.length)];
        [resultView setMainTitleAttriStr:attriStr];
        
        [resultView setBtnStr:@"刷新看看"];
        
        [resultView updateConstraintsIfNeeded];
        return resultView;
    }
    
    + (void)dismissOnView:(UIView *)view
    {
        [view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[SYLoadingResultView class]]) {
                [obj dismiss];
            }
        }];
    }
    
    - (void)dismiss
    {
        [self removeFromSuperview];
    }
    
    - (void)setContainerViewCenterY:(CGFloat)centerY
    {
        self.containerCenterY = centerY;
        [self setNeedsUpdateConstraints];
    }
    
    - (void)buttonClicked
    {
        if ([self.delegate respondsToSelector:@selector(loadingResultViewBtnClicked:)]) {
            [self.delegate loadingResultViewBtnClicked:self.button];
            [self dismiss];
            return;
        }
        if (self.clickBlock != nil) {
           self.clickBlock(self.button);
            [self dismiss];
        } else {
            [self dismiss];
        }
    }
    
    #pragma mark - Setter&Getter
    
    - (void)setUpCallBackBlock:(callBackBlock)block
    {
        if (block) {
            self.clickBlock = block;
        }
    }
    
    - (void)setMainTitleStr:(NSString *)string
    {
        self.mainLabel.text = string;
        [self setNeedsUpdateConstraints];
    }
    
    - (void)setMainTitleAttriStr:(NSAttributedString *)attriStr
    {
        self.mainLabel.attributedText = attriStr;
        [self setNeedsUpdateConstraints];
    }
    
    - (void)setSubTitleStr:(NSString *)string
    {
        self.subLabel.text = string;
        [self setNeedsUpdateConstraints];
    }
    
    - (void)setSubTitleAttriStr:(NSAttributedString *)attriStr
    {
        self.subLabel.attributedText = attriStr;
        [self setNeedsUpdateConstraints];
    }
    
    - (void)setImg:(UIImage *)image
    {
        self.imageView.image = image;
        [self setNeedsUpdateConstraints];
    }
    
    - (void)setBtnStr:(NSString *)BtnStr
    {
        [self.button setTitle:BtnStr forState:UIControlStateNormal];
        [self setNeedsUpdateConstraints];
    }
    
    - (void)setContentViewBackgroundColor:(UIColor *)backgroundColor
    {
        self.containerView.backgroundColor = backgroundColor;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS tableView无数据 占位图封装

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