forked from rickharrison/RHHorizontalTableView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RHHorizontalTableView.m
227 lines (170 loc) · 7.67 KB
/
RHHorizontalTableView.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* RHHorizontalTableView.m
* RHHorizontalTableView
*
* Created by Rick Harrison on 6/28/11.
* Copyright (c) 2011 Rick Harrison, http://rickharrison.me
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#import <objc/runtime.h>
#import "RHHorizontalTableView.h"
CGFloat const RHHorizontalTableViewScrollIndicatorPositionBottomOffset = 9.0;
@interface RHHorizontalTableView () <UITableViewDataSource, UITableViewDelegate>
@end
@implementation RHHorizontalTableView
@synthesize indicatorPosition = _indicatorPosition;
#pragma mark -
#pragma mark Initialization
- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
if ((self = [super initWithFrame:frame style:style])) {
self.transform = CGAffineTransformMakeRotation(-1 * M_PI / 2.0);
self.frame = frame;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
CGRect _beforeRotationFrame = self.frame;
self.transform = CGAffineTransformMakeRotation(-1 * M_PI / 2.0);
self.frame = _beforeRotationFrame;
}
return self;
}
#pragma mark -
#pragma mark Appearance Configuration
- (void)setIndicatorPosition:(RHHorizontalTableViewScrollIndicatorPosition)indicatorPosition {
_indicatorPosition = indicatorPosition;
if (indicatorPosition == RHHorizontalTableViewScrollIndicatorPositionTop) {
self.scrollIndicatorInsets = UIEdgeInsetsZero;
} else if (indicatorPosition == RHHorizontalTableViewScrollIndicatorPositionBottom) {
self.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, self.frame.size.height - RHHorizontalTableViewScrollIndicatorPositionBottomOffset);
}
}
#pragma mark -
#pragma mark Table View Data Source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_realDataSource tableView:tableView numberOfRowsInSection:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [_realDataSource tableView:tableView cellForRowAtIndexPath:indexPath];
if (cell) {
cell.backgroundView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
cell.selectedBackgroundView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
}
return cell;
}
#pragma mark -
#pragma mark Table View Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([_realDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)]) {
return 0.1;
}
return 0.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if ([_realDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) {
UIView *headerView = [_realDelegate tableView:tableView viewForHeaderInSection:section];
CGFloat desiredHeight = CGRectGetHeight(headerView.frame);
if ([_realDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)]) {
desiredHeight = [_realDelegate tableView:tableView heightForHeaderInSection:section];
}
headerView.frame = CGRectMake(0, 0, CGRectGetWidth(headerView.frame), desiredHeight);
/*
* Create a wrapper for the desired header view and rotate it
*/
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.autoresizingMask = UIViewAutoresizingNone;
view.transform = CGAffineTransformMakeRotation(M_PI / 2.0);
[view addSubview:headerView];
return [view autorelease];
}
return nil;
}
#pragma mark -
#pragma mark Protocol/Message Forwarding
- (BOOL)respondsToSelector:(SEL)aSelector {
BOOL result = [super respondsToSelector:aSelector];
/*
* Check if the selector is part of the UITableViewDataSource protocol.
*/
struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), aSelector, NO, YES);
if (dataSourceMethod.name != nil) {
result = [_realDataSource respondsToSelector:aSelector];
}
/*
* Check if the selector is part of the UITableViewDelegate protocol.
*/
struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), aSelector, NO, YES);
if (delegateMethod.name != nil) {
result = [_realDelegate respondsToSelector:aSelector];
}
return result;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
/*
* Check if the selector is part of the UITableViewDataSource protocol.
*/
struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), aSelector, NO, YES);
if (dataSourceMethod.name != nil) {
signature = [(NSObject *)_realDataSource methodSignatureForSelector:aSelector];
}
/*
* Check if the selector is part of the UITableViewDelegate protocol.
*/
struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), aSelector, NO, YES);
if (delegateMethod.name != nil) {
signature = [(NSObject *)_realDelegate methodSignatureForSelector:aSelector];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL selector = [anInvocation selector];
struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), selector, NO, YES);
struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), selector, NO, YES);
/*
* Route the invocation to the original data source, delegate, or super
*/
if (dataSourceMethod.name != nil && [_realDataSource respondsToSelector:selector]) {
[anInvocation invokeWithTarget:_realDataSource];
} else if (delegateMethod.name != nil && [_realDelegate respondsToSelector:selector]) {
[anInvocation invokeWithTarget:_realDelegate];
} else {
[super forwardInvocation:anInvocation];
}
}
#pragma mark -
#pragma mark Overrides
- (void)setDataSource:(id <UITableViewDataSource>)dataSource {
_realDataSource = dataSource;
[super setDataSource:self];
}
- (void)setDelegate:(id <UITableViewDelegate>)delegate {
_realDelegate = delegate;
[super setDelegate:self];
}
#pragma mark -
- (void)dealloc {
[super dealloc];
}
@end