gitHub地址:https://github.com/dzenbot/DZNEmptyDataSetgit
效果圖:github
代碼:atom
#import "UIScrollView+EmptyDataSet.h" @interface DZNEmptyDataSetViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate> @property(nonatomic,strong)UITableView *tableView; @property(nonatomic,strong)NSMutableArray *dataArr; @end @implementation DZNEmptyDataSetViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.emptyDataSetSource = self; self.tableView.emptyDataSetDelegate = self; self.tableView.tableFooterView = [UIView new]; [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArr.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; cell.textLabel.text = self.dataArr[indexPath.row]; return cell; } #pragma mark DZNEmptyDataSetSource,DZNEmptyDataSetDelegate //數據爲空的時候添加圖片提醒 - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView { return [UIImage imageNamed:@"ip_txt"]; } //數據爲空文字提醒 - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView { NSString *text = @"加載失敗請重試"; NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]}; return [[NSAttributedString alloc] initWithString:text attributes:attributes]; } //數據爲空的背景 - (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView { return [UIColor whiteColor]; } //爲空的時候添加一個風火輪 /* - (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView { UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [activityView startAnimating]; return activityView; } */ //若是tableveiw有tableHeaderView,能夠自定義展現文字的位置 - (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView { return -self.tableView.tableHeaderView.frame.size.height/1.0f; } - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView { return 20.0f; } - (BOOL) emptyDataSetShouldAllowImageViewAnimate:(UIScrollView *)scrollView { return false; } - (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view { [self.dataArr addObject:@"2"]; [self.tableView reloadData]; // Do something } -(UITableView *)tableView{ if(!_tableView){ _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; } return _tableView; } -(NSMutableArray *)dataArr{ if(!_dataArr){ _dataArr = [NSMutableArray array]; } return _dataArr; } @end
-(UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{ return [UIImage imageNamed:@"ip_txt"]; }
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button{ NSLog(@"========="); }