iOS ~ 缺省页(第三方:DZNEmptyDataSet --
2022-02-08 本文已影响0人
阳光下的叶子呵
Objective-C:
github:DZNEmptyDataSet
swift版本:
EmptyDataSet-Swift
原理:比如当UITableView的section数量为0时,显示缺省页
注意:原因是DZNEmptyDataSet的布局是autolayout(具体看DZNEmptyDataSet的 .m 文件),不是 frame 布局,所以自定义 view 时候,即使设置 frame,也不起作用,自定义的 view 没有尺寸,因此上面也不响应任何事件。
[具体解决方法就是]:(https://github.com/dzenbot/DZNEmptyDataSet/issues/405)
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//自定义的 view 用 autolayout 布局就可以了,我用的是 masonry
}
使用:
// 引入代理
<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
// tableview遵守协议
self.tableView.emptyDataSetDelegate = self;
self.tableView.emptyDataSetSource = self;
#pragma mark - 配置缺省页
//空白页是否显示
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
return YES;
}
//是否滑动
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
return YES;
}
// 自定义缺省显示视图
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{
// 设置 frame时,点击事件和设置背景颜色 不起作用
// UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
UIView *customView = [[UIView alloc] init];
customView.backgroundColor = [UIColor redColor];
[scrollView addSubview:customView];
[customView makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
}];
UIImageView *emptyDataImageView = [[UIImageView alloc]init];
[customView addSubview:emptyDataImageView];
[emptyDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(customView);
make.centerY.equalTo(customView).offset(-k_Height_TabBar-k_Height_NavBar);
make.height.offset(100);
make.width.offset(139);
}];
UILabel *emptyDataTitleLab=[[UILabel alloc]init];
emptyDataTitleLab.textAlignment=NSTextAlignmentCenter;
[customView addSubview:emptyDataTitleLab];
[emptyDataTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(customView);
make.height.offset(23);
make.width.offset(165);
make.top.equalTo(emptyDataImageView.mas_bottom).offset(15);
}];
UIButton *button = [UIButton new];
button.layer.cornerRadius = 12;
button.layer.masksToBounds = YES;
[button setTitleColor:ColorGlobalBalck forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:FontGlobalMedium size:15];
[button setBackgroundColor:ColorGlobalTheme];
[customView addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(customView);
make.height.offset(44);
make.width.offset(120);
make.top.equalTo(emptyDataTitleLab.mas_bottom).offset(23);
}];
if ([[BRNetworkHepler readNetStatus] isEqualToString:NetWork_NONET]) {
emptyDataImageView.image = [UIImage imageNamed:EmptyData_Global_NoNet_Image];
emptyDataTitleLab.text=EmptyData_Global_NoNet_TitleText;
emptyDataTitleLab.textColor = EmptyData_Global_NoNet_TitleTextColor;
emptyDataTitleLab.font = EmptyData_Global_NoNet_TitleFont;
[button setTitle:@"重试" forState:UIControlStateNormal];
[button addTarget:self action:@selector(reloadTableviewDataClick:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([[GWLocationMannger sharedGpsManager] checkLocationServicesAuthorizationStatus] == NO){
emptyDataImageView.image = [UIImage imageNamed:EmptyData_Global_NoLocation_Image];
emptyDataTitleLab.text=EmptyData_Global_NoLocation_TitleText;
emptyDataTitleLab.textColor = EmptyData_Global_NoNet_TitleTextColor;
emptyDataTitleLab.font = EmptyData_Global_NoNet_TitleFont;
[button setTitle:@"去设置" forState:UIControlStateNormal];
[button addTarget:self action:@selector(cheackLocationClick:) forControlEvents:UIControlEventTouchUpInside];
}
else{
emptyDataImageView.image = [UIImage imageNamed:EmptyData_Global_NoData_Image];
emptyDataTitleLab.text=EmptyData_Global_NoData_TitleText;
emptyDataTitleLab.textColor = EmptyData_Global_NoData_TitleTextColor;
emptyDataTitleLab.font = EmptyData_Global_NoData_TitleFont;
[button setTitle:@"刷新" forState:UIControlStateNormal];
// [button addTarget:self action:@selector(reloadUpDateClick:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(reloadTableviewDataClick:) forControlEvents:UIControlEventTouchUpInside];
}
return customView;
}
-(void)cheackLocationClick:(UIButton *)btn{
[[GWLocationMannger sharedGpsManager] checkLocationServicesAuthorizationStatus];
}
-(void)reloadTableviewDataClick:(UIButton *)btn{
[self.tableView.mj_header beginRefreshing];
}