IOS tableView EmptyView

我在做android开发的时候,ListVIew 有设置EmptyView的功能。
但是在IOS 未发现这个功能,其实很多情况下,这个功能还是比较好用的。
但是想网上找一个,也没找到让人眼睛一亮。所以自己打算写一个。

  1. 其实一开始我打算用扩展来实现这个功能,这样的话,不需要动老代码就实现了功能。 但是发现,用扩展很难完成这个功能,首先我想要 reload 的时候,判断是否有数据,是否显示emptyView。扩展的话,不好处理reload。
  2. 那么我想用,KVO的形式来判断 是否有数据,但是发现,也没啥办法能监听到数据的变化。
  3. 我只能用继承了。
#import <UIKit/UIKit.h>
@interface BaseTableView : UITableView
//Empty View
@property (nonatomic ,strong) UIView *emptyView;

@end


@interface BaseTableView(EmptyView)

-(void)showEmptyView;///<显示

-(void)dismissEmptyView;///<消失

@end


@interface TableEmptyView : UIView

@end

.m

#import "BaseTableView.h"

@implementation BaseTableView


-(void)reloadData{
    [super reloadData];

    if ([self.visibleCells count] > 0)
        [self dismissEmptyView];
    else
        [self showEmptyView];
}

@end


@implementation BaseTableView(EmptyView)


-(void)showEmptyView{
    if (!self.emptyView) {
        self.emptyView = [[TableEmptyView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
    }
    if (![self.emptyView superview]) {
        [self addSubview:self.emptyView];
        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.emptyView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:self.emptyView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    }
    self.emptyView.hidden = NO;
    [self bringSubviewToFront:self.emptyView];
}

-(void)dismissEmptyView{
    self.emptyView.hidden = YES;
    [self sendSubviewToBack:self.emptyView];
    [self.emptyView removeFromSuperview];
}

@end


@implementation TableEmptyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setTranslatesAutoresizingMaskIntoConstraints:NO];
        UIView *emptyContentView = [[UIView alloc] init];
        [emptyContentView setTranslatesAutoresizingMaskIntoConstraints:NO];
        UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty.png"]];
        [icon setTranslatesAutoresizingMaskIntoConstraints:NO];
        UILabel *lable = [[UILabel alloc] init];
        lable.text = @"还没有收到消息哦~";
        lable.textColor = [UIColor darkGrayColor];
        [lable setTranslatesAutoresizingMaskIntoConstraints:NO];

        [emptyContentView addSubview:icon];
        [emptyContentView addSubview:lable];

        NSString *Vcons = @"V:|-(1)-[icon]-(5)-[lable]-(1)-|";
        [emptyContentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:Vcons options:0 metrics:0 views:NSDictionaryOfVariableBindings(emptyContentView,icon,lable)]];

        [emptyContentView addConstraint:[NSLayoutConstraint constraintWithItem:icon attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:emptyContentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];

        [emptyContentView addConstraint:[NSLayoutConstraint constraintWithItem:lable attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:emptyContentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];


        [self addSubview:emptyContentView];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:emptyContentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];

        [self addConstraint:[NSLayoutConstraint constraintWithItem:emptyContentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
    }
    return self;
}



@end

这里写图片描述

这样就显示出来了。

不知道大家还有没有更好的方法。:(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值