本來應(yīng)該很早就寫的 姑隅, 一直覺得沒有寫這個(gè)的必要得滤,今天比較閑 想了想還是寫下這個(gè)盐肃。大多數(shù)情況下,我們的app 會(huì)出現(xiàn)沒有數(shù)據(jù)的時(shí)候誊辉,這個(gè)時(shí)候我們又不能讓用戶在哪里對(duì)著一片空白矾湃,所以我們需要做點(diǎn)事情亡脑;
.m文件中:
#import "ViewController.h"
#import "UIScrollView+EmptyDataSet.h"
#import "LeftTableViewCell.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>{
UITableView *_leftTableView;
NSMutableArray *_leftArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_leftArray = [NSMutableArray arrayWithCapacity:0];
_leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
_leftTableView.delegate = self;
_leftTableView.backgroundColor = [UIColor whiteColor];
_leftTableView.dataSource = self;
_leftTableView.emptyDataSetSource = self;
_leftTableView.emptyDataSetDelegate = self;
_leftTableView.tableHeaderView = [UIView new];
_leftTableView.tableFooterView = [UIView new];
_leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_leftTableView registerNib:[UINib nibWithNibName:@"LeftTableViewCell" bundle:nil] forCellReuseIdentifier:@"leftMessageID"];
[_leftTableView registerNib:[UINib nibWithNibName:@"LeftTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"leftTwoMessageID"];
_leftTableView.estimatedRowHeight = 44.0f;
_leftTableView.rowHeight = UITableViewAutomaticDimension;
[self.view addSubview:_leftTableView];
}
//沒有數(shù)據(jù)的時(shí)候 放一張圖片占位
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
return [UIImage imageNamed:@"icon_none_data"];
}
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
animation.duration = 0.25;
animation.cumulative = YES;
animation.repeatCount = MAXFLOAT;
return animation;
}
//點(diǎn)擊當(dāng)前數(shù)據(jù)空白處時(shí) 可以刷新
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
if (_leftTableView == scrollView) {
//開始刷新
NSLog(@"開始刷新");
}
}
//沒有數(shù)據(jù)的時(shí)候 tabbleView等 是否可以滾動(dòng)
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
return YES;
}
//沒有數(shù)據(jù)的時(shí)候添加文字提示
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
NSString *text = @"這里還什么都沒有";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
NSForegroundColorAttributeName: [UIColor lightGrayColor]};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
#pragma UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _leftArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == _leftTableView) {
LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftMessageID"];
if (!cell) {
cell = (LeftTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"leftMessageID"];
}
cell.name.text = @"";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
return nil;
}
@end
效果就是下面的圖:
===========================================
這里是demo:??