美文网首页
DZNEmptyDataSet:页面空数据展示方案

DZNEmptyDataSet:页面空数据展示方案

作者: 囧书 | 来源:发表于2016-08-05 11:32 被阅读264次

在页面数据为空的情况下,往往需要配置图文展示以代替整页的空白

DZNEmptyDataSet对UIScrollView作了扩展,自动监测当UIScrollView的子类例如UITableView无内容时,就会显示配置好的空数据图文。

EmptyDataSetDemo.gif

用法

#import "UIScrollView+EmptyDataSet.h"

@interface ViewController () <UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSString *> *dataSource;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"EmptyDataSetDemo";
    
    self.tableView.frame = self.view.bounds;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [self.dataSource addObjectsFromArray:[self strArray]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"生成" style:UIBarButtonItemStylePlain target:self action:@selector(onLeft)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"清空" style:UIBarButtonItemStylePlain target:self action:@selector(onRight)];
}

- (void)onLeft {
    [self.dataSource addObjectsFromArray:[self strArray]];
    [self.tableView reloadData];
}

- (void)onRight {
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        // 设置emptyDataSet代理
        _tableView.emptyDataSetDelegate = self;
        _tableView.emptyDataSetSource = self;
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (NSMutableArray<NSString *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSArray<NSString *> *)strArray {
    return @[@"AAA", @"BBB", @"CCC"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    cell.textLabel.text = self.dataSource[indexPath.row];
    return cell;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"empty.png"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"标题标题标题";
    NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:14],
                           NSForegroundColorAttributeName : [UIColor darkGrayColor]};
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:text attributes:attr];
    return attStr;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"夕阳照耀下的花岗岩山地仿佛披上了一层薄薄的金纱,眼睛所及之处都是不可言喻的美景。各种野生动物在昼夜交接的夕阳下活动,达特穆尔国家公园总会给人不虚此行的实感。";
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, text.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, text.length)];
    return attributedString;
}

相关文章

网友评论

      本文标题:DZNEmptyDataSet:页面空数据展示方案

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