DZNEmptyDataSet 使用

时间:2023-03-09 15:16:31
DZNEmptyDataSet 使用

gitHub地址:https://github.com/dzenbot/DZNEmptyDataSet

也可以使用DZNEmptyDataSet

效果图:

DZNEmptyDataSet 使用

代码:

#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(@"=========");
}