-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIScrollView+WDNoData.m
More file actions
103 lines (81 loc) · 3.37 KB
/
UIScrollView+WDNoData.m
File metadata and controls
103 lines (81 loc) · 3.37 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// UIScrollView+WDNoData.m
// Test
//
// Created by wudan on 2018/12/25.
// Copyright © 2018 wudan. All rights reserved.
//
#import "UIScrollView+WDNoData.h"
#import <objc/runtime.h>
static char *noDataViewKey = "noDataViewKey";
@implementation UIScrollView (WDNoData)
void exchangeSelector(SEL sel1, SEL sel2, Class cls) {
Class class = [cls class];
Method originalMethod = class_getInstanceMethod(class, sel1);
Method swizzledMethod = class_getInstanceMethod(class, sel2);
BOOL success = class_addMethod(class, sel1, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(class, sel2, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
#pragma mark =============== Setter ===============
- (void)setNoDataView:(UIView *)noDataView {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
exchangeSelector(@selector(reloadData), @selector(wd_reloadData), [self class]);
});
objc_setAssociatedObject(self, noDataViewKey, noDataView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark =============== Getter ===============
- (UIView *)noDataView {
UIView *noDataView = objc_getAssociatedObject(self, noDataViewKey);
noDataView.frame = self.frame;
return noDataView;
}
- (void)wd_reloadData {
[self wd_reloadData];
[self wd_checkData];
}
#pragma mark =============== 获取数据 ===============
- (void)wd_checkData {
NSInteger items = 0;
if (![self respondsToSelector:@selector(dataSource)]) {
return;
}
// UITableView support
if ([self isKindOfClass:[UITableView class]]) {
UITableView *tableView = (UITableView *)self;
id <UITableViewDataSource> dataSource = tableView.dataSource;
NSInteger sections = 1;
if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
sections = [dataSource numberOfSectionsInTableView:tableView];
}
if (dataSource && [dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
for (NSInteger section = 0; section < sections; section++) {
items += [dataSource tableView:tableView numberOfRowsInSection:section];
}
}
}
// UICollectionView support
else if ([self isKindOfClass:[UICollectionView class]]) {
UICollectionView *collectionView = (UICollectionView *)self;
id <UICollectionViewDataSource> dataSource = collectionView.dataSource;
NSInteger sections = 1;
if (dataSource && [dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
sections = [dataSource numberOfSectionsInCollectionView:collectionView];
}
if (dataSource && [dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
for (NSInteger section = 0; section < sections; section++) {
items += [dataSource collectionView:collectionView numberOfItemsInSection:section];
}
}
}
if ( items == 0 ) {
[self addSubview:self.noDataView];
} else {
[self.noDataView removeFromSuperview];
}
}
@end