forked from gabrielPeart/RRNCollapsableSectionTableView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRRNCollapsableSectionTableViewController.m
194 lines (130 loc) · 6.82 KB
/
RRNCollapsableSectionTableViewController.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// RRNCollapsableTableViewController.m
// RRNCollapsableSectionTableView
//
// Created by Robert Nash on 08/09/2015.
// Copyright (c) 2015 Robert Nash. All rights reserved.
//
#import "RRNCollapsableSectionTableViewController.h"
#import "RRNCollapsableSectionItemProtocol.h"
@interface RRNCollapsableTableViewController ()
@end
@implementation RRNCollapsableTableViewController
-(void)viewDidLoad {
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:[self sectionHeaderNibName] bundle:nil];
[[self collapsableTableView] registerNib:nib forHeaderFooterViewReuseIdentifier:[self sectionHeaderReuseIdentifier]];
}
-(UITableView *)collapsableTableView {
return nil;
}
-(NSArray *)model {
return nil;
}
-(BOOL)singleOpenSelectionOnly {
return NO;
}
-(NSString *)sectionHeaderNibName {
return nil;
}
-(NSString *)sectionHeaderReuseIdentifier {
return [[self sectionHeaderNibName] stringByAppendingString:@"ID"];
}
#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self model].count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id menuSection = [[self model] objectAtIndex:section];
BOOL itemConforms = [menuSection conformsToProtocol:@protocol(RRNCollapsableSectionItemProtocol)];
return (itemConforms && ((id <RRNCollapsableSectionItemProtocol>)menuSection).isVisible.boolValue) ? ((id <RRNCollapsableSectionItemProtocol>)menuSection).items.count : 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
id menuSection = [[self model] objectAtIndex:section];
UIView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:[self sectionHeaderReuseIdentifier]];
view.tag = section;
BOOL headerConforms = [view conformsToProtocol:@protocol(RRNCollapsableSectionHeaderProtocol)];
if (headerConforms) {
((id <RRNCollapsableSectionHeaderProtocol>)view).interactionDelegate = self;
}
BOOL itemConforms = [menuSection conformsToProtocol:@protocol(RRNCollapsableSectionItemProtocol)];
if (headerConforms && itemConforms) {
((id <RRNCollapsableSectionHeaderProtocol>)view).titleLabel.text = ((id <RRNCollapsableSectionItemProtocol>)menuSection).title;
}
return view;
}
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
id menuSection = [[self model] objectAtIndex:section];
BOOL headerConforms = [view conformsToProtocol:@protocol(RRNCollapsableSectionHeaderProtocol)];
BOOL itemConforms = [menuSection conformsToProtocol:@protocol(RRNCollapsableSectionItemProtocol)];
if (headerConforms && itemConforms && ((id <RRNCollapsableSectionItemProtocol>)menuSection).isVisible.boolValue) {
[((id <RRNCollapsableSectionHeaderProtocol>)view) openAnimated:NO];
} else if (headerConforms) {
[((id <RRNCollapsableSectionHeaderProtocol>)view) closeAnimated:NO];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}
#pragma mark - ReactiveSectionHeaderProtocol
-(void)userTapped:(UIView *)view {
UITableView *tableView = [self collapsableTableView];
[tableView beginUpdates];
BOOL foundOpenUnchosenMenuSection = NO;
NSArray *menu = [self model];
for (id <RRNCollapsableSectionItemProtocol> menuSection in menu) {
if (![menuSection conformsToProtocol:@protocol(RRNCollapsableSectionItemProtocol)]) {
continue;
}
BOOL chosenMenuSection = menuSection == [menu objectAtIndex:view.tag];
BOOL isVisible = menuSection.isVisible.boolValue;
if (isVisible && chosenMenuSection) {
menuSection.isVisible = @NO;
BOOL headerConforms = [view conformsToProtocol:@protocol(RRNCollapsableSectionHeaderProtocol)];
if (headerConforms) {
[((id <RRNCollapsableSectionHeaderProtocol>)view) closeAnimated:YES];
}
NSInteger section = view.tag;
NSArray *indexPaths = [self indexPathsForSection:section
forMenuSection:menuSection];
[tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:(foundOpenUnchosenMenuSection) ? UITableViewRowAnimationBottom : UITableViewRowAnimationTop];
} else if (!isVisible && chosenMenuSection) {
menuSection.isVisible = @YES;
BOOL headerConforms = [view conformsToProtocol:@protocol(RRNCollapsableSectionHeaderProtocol)];
if (headerConforms) {
[((id <RRNCollapsableSectionHeaderProtocol>)view) openAnimated:YES];
}
NSInteger section = view.tag;
NSArray *indexPaths = [self indexPathsForSection:section
forMenuSection:menuSection];
[tableView insertRowsAtIndexPaths:indexPaths
withRowAnimation:(foundOpenUnchosenMenuSection) ? UITableViewRowAnimationBottom : UITableViewRowAnimationTop];
} else if (isVisible && !chosenMenuSection && [self singleOpenSelectionOnly]) {
foundOpenUnchosenMenuSection = YES;
menuSection.isVisible = @NO;
NSInteger section = [menu indexOfObject:menuSection];
UIView *headerView = [tableView headerViewForSection:section];
BOOL headerConforms = [view conformsToProtocol:@protocol(RRNCollapsableSectionHeaderProtocol)];
if (headerConforms) {
[((id <RRNCollapsableSectionHeaderProtocol>)headerView) closeAnimated:YES];
}
NSArray *indexPaths = [self indexPathsForSection:section
forMenuSection:menuSection];
[tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:(view.tag > section) ? UITableViewRowAnimationTop : UITableViewRowAnimationBottom];
}
}
[tableView endUpdates];
}
-(NSArray *)indexPathsForSection:(NSInteger)section forMenuSection:(id <RRNCollapsableSectionItemProtocol>)menuSection {
NSMutableArray *collector = [NSMutableArray new];
NSInteger count = menuSection.items.count;
NSIndexPath *indexPath;
for (NSInteger i = 0; i < count; i++) {
indexPath = [NSIndexPath indexPathForRow:i inSection:section];
[collector addObject:indexPath];
}
return [collector copy];
}
@end