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;
}

DZNEmptyDataSet的使用的更多相关文章

  1. DZNEmptyDataSet,优秀的空白页或者出错页封装

    简介 项目主页:https://github.com/dzenbot/DZNEmptyDataSet 提示:主要用于UITableView和UICollectionView,也可以用于UIScroll ...

  2. DZNEmptyDataSet框架简介

    给大家推荐一个设置页面加载失败时显示加载失败等的框架. 下载地址:DZNEmptyDataSet https://github.com/dzenbot/DZNEmptyDataSet 上效果首先在你的 ...

  3. DZNEmptyDataSet 使用

    gitHub地址:https://github.com/dzenbot/DZNEmptyDataSet 效果图: 代码: #import "UIScrollView+EmptyDataSet ...

  4. DZNEmptyDataSet——空白数据集显示框架

    GitHub地址:DZNEmptyDataSet DZNEmptyDataSet DZNEmptyDataSet 是基于 UITableView/UICollectionView 的范畴/扩展(cat ...

  5. DZNEmptyDataSet框架阅读

      前段时间使用公司封装的空白页占位视图工具,工具是对DZNEmptyDataSet框架的封装.这个框架以前在许多项目也都用过,却没有认真阅读过源码,真的很遗憾.这两天趁五一放假有空,将DZNEmpt ...

  6. iOS比较常用的第三方及实例(不断更新中)

    把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新: 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...

  7. IOS比较常用的第三方组件及应用源代码(持续更新中)

    把平时看到或项目用到的一些插件进行整理,文章后面分享一些不错的实例,若你有其它的插件欢迎分享,不断的进行更新~ 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com ...

  8. ios项目里扒出来的json文件

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  9. iOS 常用第三方类库、完整APP示例

    一.第三方类库 1:基于响应式编程思想的oc地址:https://github.com/ReactiveCocoa/ReactiveCocoa2:hud提示框地址:https://github.com ...

随机推荐

  1. CF 1096D Easy Problem [动态规划]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 有一长度为n的字符串,每一字符都有一个权值,要求现在从中取出若干个字符,使得字符串中没 ...

  2. VMware12 + Ubuntu16.04 虚拟磁盘扩容

    转载自:https://blog.csdn.net/Timsley/article/details/50742755 今天用虚拟机的时候,发现虚拟机快满了,提示磁盘空间小,不得不扩充虚拟机空间.经过百 ...

  3. 封装FTP类

    using System; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; using ...

  4. Tarjan算法:求解无向连通图图的割点(关节点)与桥(割边)

    1. 割点与连通度 在无向连通图中,删除一个顶点v及其相连的边后,原图从一个连通分量变成了两个或多个连通分量,则称顶点v为割点,同时也称关节点(Articulation Point).一个没有关节点的 ...

  5. [Algo] 223. Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  6. C语言入门基础整理

    学习计算机技术,C语言可以说是必备的,他已经成为现在计算机行业人学习必备的,而且应用也是十分的广泛,今天就来看看拥有几年c语言工作经验的大神整理的C语言入门基础知识,没有学不会,只有不肯学. 结构化程 ...

  7. oBike退出新加坡、ofo取消免押金服务,全球共享单车都怎么了?

    浪潮退去后,才知道谁在裸泳.这句已经被说烂的"至理名言",往往被用在一波接一波的互联网热潮中.团购.O2O.共享单车.共享打车.无人货柜--几乎每一波热潮在退去后会暴露出存在的问题 ...

  8. 31)PHP,对象的遍历

    对象的遍历: 对象也可以可以使用foreach语句进行便利,有两点注意: 1,只能便利属性.(所以,这个就解决了,为啥之前的数据库类,我只是看到了一些属性名字,而没有得到我的属性值) 2,只能便利“看 ...

  9. 吴裕雄--天生自然 JAVA开发学习:网络编程

    import java.net.*; import java.io.*; public class GreetingClient { public static void main(String [] ...

  10. day13-面向对象

    #解决同一类问题,使用面向对象的思想.类是制造对象的模具,类是抽象的,我们能知道它有哪些属性(name,age,saraly),但不知道具体的属性值. #看下面代码:类Penson制造了实例化对象re ...