forked from steipete/PSFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UITableView+FKLoading.m
61 lines (44 loc) · 1.73 KB
/
UITableView+FKLoading.m
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
#import "UITableView+FKLoading.h"
#import "UITableViewCell+FKLoading.h"
#import "NSObject+AssociatedObjects.h"
static char allowsMultipleIndicatorsKey;
static char cellsShowingIndicatorKey;
@interface UITableView ()
@property (nonatomic, readonly) NSMutableSet *cellsShowingLoadingIndicator;
@end
@implementation UITableView (FKLoading)
- (void)showLoadingIndicatorAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
if (!self.allowsMultipleLoadingIndicators) {
[self hideLoadingIndicators];
}
[cell showLoadingIndicator];
[self.cellsShowingLoadingIndicator addObject:indexPath];
}
- (void)hideLoadingIndicatorAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
[cell hideLoadingIndicator];
[self.cellsShowingLoadingIndicator removeObject:indexPath];
}
// hides all loading indicators
- (void)hideLoadingIndicators {
for (NSIndexPath *indexPath in self.cellsShowingLoadingIndicator) {
[self hideLoadingIndicatorAtIndexPath:indexPath];
}
}
- (void)setAllowsMultipleLoadingIndicators:(BOOL)allowsMultipleLoadingIndicators {
[self associateValue:$B(allowsMultipleLoadingIndicators) withKey:&allowsMultipleIndicatorsKey];
}
- (BOOL)allowsMultipleLoadingIndicators {
return [[self associatedValueForKey:&allowsMultipleIndicatorsKey] boolValue];
}
- (NSMutableSet *)cellsShowingLoadingIndicator {
NSMutableSet *cells = (NSMutableSet *)[self associatedValueForKey:&cellsShowingIndicatorKey];
// lazy loading
if (cells == nil) {
cells = [NSMutableSet set];
[self associateValue:cells withKey:&cellsShowingIndicatorKey];
}
return cells;
}
@end