-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRACTableViewDataSource.m
48 lines (36 loc) · 1.59 KB
/
RACTableViewDataSource.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
#import "RACTableViewCell.h"
#import "RACTableViewDataSource.h"
#import <ReactiveCocoa/ReactiveCocoa.h>
@implementation RACTableViewDataSource
- (instancetype)initWithSource:(RACSignal *)source tableView:(UITableView *)tableView andReuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super init]) {
_tableView = tableView;
_reuseIdentifier = reuseIdentifier;
_data = @[];
RAC(self, data) = [[source ignore:nil] doNext:^(NSArray *data) {
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView reloadData];
});
}];
_tableView.dataSource = self;
}
return self;
}
+ (instancetype)dataSource:(RACSignal *)signal tableView:(UITableView *)tableView andReuseIdentifier:(NSString *)reuseIdentifier {
return [[RACTableViewDataSource alloc] initWithSource:signal tableView:tableView andReuseIdentifier:reuseIdentifier];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_reuseIdentifier];
NSObject *data = self.data[indexPath.row];
if ([cell conformsToProtocol:@protocol(RACTableViewCell) ]) {
[(UITableViewCell<RACTableViewCell> *)cell prepareToAppear:data];
} else if ([data isKindOfClass:[NSString class]]) {
cell.textLabel.text = (NSString *)data;
}
return cell;
}
@end