DZNEmptyDataSet的使用

DZNEmptyDataSet是外国友人写的开源项目,github地址(具体的使用以及Demo,点击进入github主页),简单介绍下DZNEmptyDataSet的使用方法。

对于iOS开发者来说,UITableView是在开发过程中使用比较多的一个控件。如果UITableView表视图没有数据,页面一片空白,不是一个很好的用户体验。我们都希望在数据源为空的时候,给用户一些相应的提示,提高交互效果。

DZNEmptyDataSet就如同福音,适用于每一个iOS项目,只要遵DZNEmptyDataSetSource、 DZNEmptyDataSetDelegate这两个协议,在UITableView和UICollectionView中实现对应的代理方法就OK啦。

DZNEmptyDataSet效果图

一、函数调用

1.1 数据源方法

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      
      
* 返回占位图图片
*/
- ( 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大专栏  DZNEmptyDataSet的使用an>;
}

1.2 代理方法

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
      
      
* 数据源为空时是否渲染和显示 (默认为 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 ReloadData

当数据源发生变化是执行:

      
      
1
      
      
[ self.tableView reloadData];

或者

      
      
1
      
      
[ self.collectionView reloadData];

二、项目中使用

2.1 实际使用的方法

      
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
      
      
- ( void)emptyDataSetWillAppear:( UIScrollView *)scrollView {
self.mTableView.contentOffset = CGPointZero;
}
- ( UIImage *)imageForEmptyDataSet:( UIScrollView *)scrollView {
return [ UIImage imageNamed: @"icon_can_not_find_the_network"];
}
- ( NSAttributedString *)buttonTitleForEmptyDataSet:( UIScrollView *)scrollView forState:( UIControlState)state {
NSString *text = @"网络不给力,请点击重试哦~";
UIFont *font = [ UIFont systemFontOfSize: 15.0];
NSMutableAttributedString *attStr = [[ NSMutableAttributedString alloc] initWithString:text];
[attStr addAttribute: NSFontAttributeName value:font range: NSMakeRange( 0, text.length)];
[attStr addAttribute: NSForegroundColorAttributeName value:kTextLightColor range: NSMakeRange( 0, text.length)];
[attStr addAttribute: NSForegroundColorAttributeName value:kThemeColor range: NSMakeRange( 7, 4)];
return attStr;
}
- ( void)emptyDataSet:( UIScrollView *)scrollView didTapButton:( UIButton *)button {
// 点击重试
}
- ( CGFloat)verticalOffsetForEmptyDataSet:( UIScrollView *)scrollView {
return -70.0f;
}

2.2 效果图

2.3 遇到的问题

在使用过程中发现有时候出现无数据或者无网络的情况,占位图的位置并不是在屏幕设定的位置,会出现偏移的现象。

解决思路:

在emptyDataSetWillAppear方法中,将tableView或者scrollerView的contentOffset设为 CGPointZero

      
      
1
2
3
      
      
- ( void)emptyDataSetWillAppear:( UIScrollView *)scrollView {
self.mTableView.contentOffset = CGPointZero;
}

猜你喜欢

转载自www.cnblogs.com/lijianming180/p/12302780.html