阅读DZNEmptyDataSet源码笔记

组成

分类:

  • UIScrollView (DZNEmptyDataSet)
  • UIView (DZNConstraintBasedLayoutExtensions)

协议:

  • DZNEmptyDataSetSource
  • DZNEmptyDataSetDelegate

类:

  • DZNEmptyDataSetView
  • DZNWeakObjectContainer

UIScrollView (DZNEmptyDataSet)

使用运行时添加的属性:

  • emptyDataSetSource
  • emptyDataSetDelegate
  • isEmptyDataSetVisible

使用runtime Swizzling为reloadData添加方法:

  • dzn_reloadEmptyDataSet

    即当tableView或collectionView 调用reloadData方法时会执行dzn_reloadEmptyDataSet。这是整个框架的核心方法。

dzn_reloadEmptyDataSet方法

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
- (void)dzn_reloadEmptyDataSet
{
//1.是否能显示 empty dataset view
if (![self dzn_canDisplay]) {
return;
}
//2.一、询问emptyDataSetDelegate是否应该显示 二、itemsCount是否为0:tableView根据tableView:numberOfRowsInSection方法,collectionView根据 collectionView: numberOfItemsInSection:方法
if (([self dzn_shouldDisplay] && [self dzn_itemsCount] == 0) || [self dzn_shouldBeForcedToDisplay])
{
//3.创建empty dataset view(空数据View)
// Notifies that the empty dataset view will appear
//3.1通知emptyDataSetDelegate代理,dataset view即将显示
[self dzn_willAppear];
//3.2 创建
DZNEmptyDataSetView *view = self.emptyDataSetView;
if (!view.superview) {
//3.3添加到ScrollView
// Send the view all the way to the back, in case a header and/or footer is present, as well as for sectionHeaders or any other content
if (([self isKindOfClass:[UITableView class]] || [self isKindOfClass:[UICollectionView class]]) && self.subviews.count > 1) {
[self insertSubview:view atIndex:0];
}
else {
[self addSubview:view];
}
}
//3.4 清理,重置约束以
// Removing view resetting the view and its constraints it very important to guarantee a good state
[view prepareForReuse];
//3.5 添加你提供的自定义View
UIView *customView = [self dzn_customView];
// If a non-nil custom view is available, let's configure it instead
if (customView) {
view.customView = customView;
}
else {
// Get the data from the data source
//3.6 根据emptyDataSetSource提供的数据,配置空数据View的子View
//具体代码太长,为避免干扰省略
//3.7 通知emptyDataSetDelegate 空数据View已显示
// Notifies that the empty dataset view did appear
[self dzn_didAppear];
}
else if (self.isEmptyDataSetVisible) {
//4.空数据View清除操作
[self dzn_invalidate];
}
}