DZNEmptyDataSet 是一个 UITableView / UICollectionView 的空白数据显示库。
DZNEmptyDataSet效果图

安装
pod 'DZNEmptyDataSet'
使用
1.0 继承协议、设定代理
// 继承协议
@interface RefuelVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
// 设定代理
self.tableView.emptyDataSetDelegate = self;
self.tableView.emptyDataSetSource = self;
1.1 数据源方法
/**
* 返回占位图图片
*/
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
[UIImage imageNamed:@""];
}
/**
* 返回标题文字
*/
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
NSString *text = @"No Application Found";
return [[NSAttributedString alloc] initWithString:text attributes:nil];
}
/**
* 返回详情文字
*/
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
UISearchBar *searchBar = self.searchDisplayController.searchBar;
NSString *text = [NSString stringWithFormat:@"There are no empty dataset examples for \"%@\".", searchBar.text];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:nil];
[attributedString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17.0] range:[attributedString.string rangeOfString:searchBar.text]];
return attributedString;
}
/**
* 返回文字按钮
*/
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
NSString *text = @"Search on the App Store";
UIFont *font = [UIFont systemFontOfSize:16.0];
UIColor *textColor = [UIColor colorWithHex:(state == UIControlStateNormal) ? @"007aff" : @"c6def9"];
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:textColor forKey:NSForegroundColorAttributeName];
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
/**
* 返回图片按钮(如果设置了此方法,buttonTitleForEmptyDataSet: ,返回文字按钮方法就会失效
*/
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
return [UIImage imageNamed:@""];
}
/**
* 自定义背景颜色
*/
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
return [UIColor whiteColor];
}
/**
* 自定义视图 (关键方法,可以做一些自定义占位视图)
*/
//- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//
//}
/**
* 设置垂直或者水平方向的偏移量,推荐使用verticalOffsetForEmptyDataSet这个方法
*
* @return 返回对应的偏移量(默认都为0)
*/
- (CGPoint)offsetForEmptyDataSet:(UIScrollView *)scrollView {
return CGPointMake(0, -64.0);
}
/**
* 设置垂直方向的偏移量 (推荐使用)
*/
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
return -64.0;
}
1.2 代理方法
/**
* 数据源为空时是否渲染和显示 (默认为 YES)
*/
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
return YES;
}
/**
* 是否允许点击 (默认为 YES)
*/
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
return YES;
}
/**
* 是否允许滚动 (默认为 NO)
*/
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
return YES;
}
/**
* 处理空白区域的点击事件
*/
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
NSLog(@"%s",__FUNCTION__);
}
/**
* 处理按钮的点击事件
*/
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
UISearchBar *searchBar = self.searchDisplayController.searchBar;
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.com/apps/%@", searchBar.text]];
if ([[UIApplication sharedApplication] canOpenURL:URL]) {
[[UIApplication sharedApplication] openURL:URL];
}
}
1.3 数据刷新时机
当数据源发生变化是执行:
[self.tableView reloadData];
or
[self.collectionView reloadData];
网友评论