DZNEmptyDataSet主要应用于没有数据时的tableView,或者其他的scrollview
当用户正在请求数据,或者没有数据时,显示一个空白的页面很不友好,用户可能会一脸懵逼的不知道接下来要干什么,这时候DZNEmptyDataSet作用就出来了,给一张图片提示或者文字提示,用户的好感会大大提升滴~
第一步:安装,只有一个分类
pod 'DZNEmptyDataSet'
image.png
第二步:使用
@interface ViewController () <UITableViewDelegate, UITableViewDataSource,
DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
_tableView.emptyDataSetSource = self;
_tableView.emptyDataSetDelegate = self;
// 删除cell分割线,不然没有数据时会显示分割线
_tableView.tableFooterView = [UIView new];
第三步:代理方法
只返回一张图片的提示
// 返回空白页的图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
return [UIImage imageNamed:@"empty"];
}
效果如下:
image.png
返回一张图片的提示+文字的提示
// 返回文字的提示
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
NSString *title = @"数据为空,请稍后再试~";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:20],
NSForegroundColorAttributeName: [UIColor redColor]
};
NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:title attributes:attributes];
return attributedStr;
}
效果如下:
image.png
返回一张图片的提示+文字的提示+描述
// 返回文字的描述
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
NSString *title = @"哈哈,服务器宕机了,卸载算了^_^";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor lightGrayColor]
};
NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:title attributes:attributes];
return attributedStr;
}
效果如下:
image.png
返回一张图片的提示+文字的提示+描述+按钮
// 按钮的文字
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
NSString *title = @"我是一个按钮";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor redColor]
};
NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:title attributes:attributes];
return attributedStr;
}
// 默认的按钮点击事件
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
NSLog(@"默认的按钮点击");
}
效果如下:
image.png
按钮设置图片
// 按钮设置图片
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
return [UIImage imageNamed:@"empty"];
}
按钮设置背景图片
// 按钮设置背景图片
- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
return [UIImage imageNamed:@"empty"];
}
设置颜色
// 设置图片的颜色,设置了就没有图片了,就是一个色块。。。。
- (UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView{
return [UIColor redColor];
}
// 设置空白页面的背景色
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{
return [UIColor greenColor];
}
自定义视图
// 自定义显示视图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{
UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 0, self.view.frame.size.width-100, 44)];
button.backgroundColor=[UIColor redColor];
[button setTitle:@"我是按钮" forState:UIControlStateNormal];
button.layer.cornerRadius = 10;
button.layer.masksToBounds = YES;
[button addTarget:self action:@selector(sliding) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2, 50, 50, 50)];
imageView.image=[UIImage imageNamed:@"empty"];
[customView addSubview:imageView];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 20)];
label.text=@"这是自定义视图";
label.textAlignment=NSTextAlignmentCenter;
[customView addSubview:label];
return customView;
}
- (void)sliding {
NSLog(@"sliding");
}
效果如下
image.png