-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRCTTableView.m
97 lines (71 loc) · 2.78 KB
/
RCTTableView.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
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
#import "RCTTableView.h"
#import "RCTConvert.h"
#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "RCTUIManager.h"
#import "RCTUtils.h"
#import "UIView+React.h"
static NSString * const CellIdentifier = @"Cell";
@interface RCTTableView () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) RCTEventDispatcher *eventDispatcher;
@property (strong, nonatomic) UITableView *tableView;
@end
@implementation RCTTableView
- (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher {
if ((self = [super initWithFrame:CGRectZero])) {
_eventDispatcher = eventDispatcher;
}
return self;
}
#pragma mark - Properties
- (void)setTableViewStyle:(UITableViewStyle)tableViewStyle {
_tableViewStyle = tableViewStyle;
[self createTableView];
}
#pragma mark -
- (void)layoutSubviews {
// Maybe something better
[self.tableView setFrame:self.frame];
}
#pragma mark - Private APIs
- (void)createTableView {
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:_tableViewStyle];
_tableView.dataSource = self;
_tableView.delegate = self;
[self addSubview:_tableView];
}
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *model = self.cellForRowAtIndexPath[[indexPath section]][[indexPath row]];
// TODO: Refactor
for (NSString *propertyName in model) {
for (NSString *subPropertyName in model[propertyName]) {
NSString *keyPath = [NSString stringWithFormat:@"%@.%@", propertyName, subPropertyName];
[cell setValue:[model valueForKeyPath:keyPath] forKeyPath:keyPath];
}
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.cellForRowAtIndexPath count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.cellForRowAtIndexPath[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) cell = [[UITableViewCell alloc] initWithStyle:self.tableViewCellStyle reuseIdentifier:CellIdentifier];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *event = @{
@"target": self.reactTag,
@"indexPath": @{
@"section": @([indexPath section]),
@"row": @([indexPath row])
}
};
[self.eventDispatcher sendInputEventWithName:@"topTap" body:event];
}
@end