iOS CodingiOS精华文章iOS程序猿

一行代码集成表空白视图

2017-01-12  本文已影响286人  minjing_lin
茜茜女神

iOS开发过程中,当UITableView的数据源为空的时候,为了产品有良好的用户体验,常常会显示一个含有友好提示功能的空白页面。那么,问题来了,你是怎么实现的那?

之前一直用的是DZNEmptyDataSet,感觉也挺简单,只需实现DZNEmptyDataSetSourceDZNEmptyDataSetDelegate中的代理方法即可。
优点:协议方法比较多,常用的功能都已集成进去。参考资料
缺点:格式已经写死,自定义的时候,不方便。

今天我要介绍的就是UIView分类利用Runtime来一步步实现空白视图~~~

@interface MJBlankPageView : UIView
@end
@interface UIView (MJEmptyView)
//MJBlankPageView
@property (nonatomic,strong) MJBlankPageView* blankPageView;
-(void)showBlankPageView;
-(void)hideBlankPageView;

@end
- (void)setBlankPageView:(MJBlankPageView *)blankPageView{
    [self willChangeValueForKey:NSStringFromSelector(@selector(blankPageView))];
    objc_setAssociatedObject(self, @selector(blankPageView), blankPageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:NSStringFromSelector(@selector(blankPageView))];
}
- (MJBlankPageView *)blankPageView{
    return objc_getAssociatedObject(self, _cmd);
}

objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)

id object : 表示关联者,是一个对象
const void key: 获取被关联者的索引key
id value : 被关联者
objc_AssociationPolicy policy : 关联时采用的协议,有assign,retain,copy等协议,一般使用OBJC_ASSOCIATION_RETAIN_NONATOMIC

- (void)showBlankPageView{
    
    if (!self.blankPageView) {
        self.blankPageView = [[MJBlankPageView alloc]initWithFrame:self.bounds];
    }
    [self addSubview:self.blankPageView];
    [self bringSubviewToFront:self.blankPageView];
}
- (void)hideBlankPageView{
    if (self.blankPageView) {
        [self.blankPageView removeFromSuperview];
        self.blankPageView = nil;
    }
}
@interface MJBlankPageView ()
@property (nonatomic,weak) UIImageView* nodataImageView;
@property (nonatomic,weak) UILabel* nodataTipLabel;
@end

@implementation MJBlankPageView
- (instancetype)init{
    return [self initWithFrame:CGRectZero];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView* nodataImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nilNetWork"]];
        _nodataImageView = nodataImageView;
        [self addSubview:_nodataImageView];
        
        UILabel* nodataTipLabel = [[UILabel alloc]init];
        nodataTipLabel.numberOfLines = 1;
        nodataTipLabel.font = [UIFont systemFontOfSize:15];
        nodataTipLabel.textAlignment = NSTextAlignmentCenter;
        nodataTipLabel.textColor = [UIColor grayColor];
        nodataTipLabel.text = @"这里没有数据呢,赶紧弄出点动静吧~";
        _nodataTipLabel = nodataTipLabel;
        [self addSubview:_nodataTipLabel];
        
        [_nodataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.bottom.equalTo(self.mas_centerY).offset(-10);
        }];
        
        [_nodataTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@50);
            make.top.equalTo(_nodataImageView.mas_bottom).offset(5);
        }];
    }
    return self;
}
@end
[self.view showBlankPageView];
// 隐藏
// [self.view hideBlankPageView];
空白视图

~~~
那如果有按钮呐?比如说网路错误需要刷新,这个必须的考虑到了。这时只需要添加一个block方法,动态关联block属性,即可。

1.添加block方法

- (void)configReloadAction:(void(^)())block;

2.动态添加block属性

@interface UIView ()
@property (nonatomic,copy) void(^reloadAction)();
@end
@implementation UIView (MJEmptyView)
- (void)setReloadAction:(void (^)())reloadAction{
    objc_setAssociatedObject(self, @selector(reloadAction), reloadAction, OBJC_ASSOCIATION_COPY);
}
- (void (^)())reloadAction{
    return objc_getAssociatedObject(self, _cmd);
}
@end

3.实现方法

- (void)configReloadAction:(void (^)())block{
    self.reloadAction = block;
    if (self.errorPageView && self.reloadAction) {
        self.errorPageView.didClickReloadBlock = self.reloadAction;
    }
}

4.调用方法

  [self.view configReloadAction:^{
        NSLog(@"1111");
    }];
无网络视图+刷新按钮

奉上代码,喜欢请点赞,有错误请反馈,共同进步~~~
【https://github.com/JingJing-Lin/MJEmptyView】

上一篇下一篇

猜你喜欢

热点阅读