├── README.md ├── RHHorizontalTableView.h └── RHHorizontalTableView.m /README.md: -------------------------------------------------------------------------------- 1 | # RHHorizontalTableView 0.1.0 2 | 3 | RHHorizontalTableView is a subclass of `UITableView`, which is a part of `UIKit`. Instead of scrolling vertically, it scrolls horizontally and loads in new cells as they come onto the screen from the left or right. 4 | 5 | # Guidelines 6 | 7 | Before implementation, make sure you understand the following: 8 | 9 | 1. The height of the rows will be what you set as the height property for the table view 10 | 2. Section headers will appear in the top left of the table view with the width and height as returned by the delegate methods. Instead of being pushed off the screen, subsequent section headers will scroll over top of other headers. 11 | 3. You can either have the scroll bar be at the top or the bottom of the table view using the `indicatorPosition` property. Possible values are: 12 | - `RHHorizontalTableViewScrollIndicatorPositionTop` 13 | - `RHHorizontalTableViewScrollIndicatorPositionBottom` 14 | 15 | # Usage 16 | 17 | 1. Clone or otherwise copy `RHHorizontalTableView.h` and `RHHorizontalTableView.m` into your project. 18 | 2. Create a `RHHorizontalTableView` either in Interface Builder or in code with `- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style` 19 | 3. Implement the data source/delegate methods as you would usually with one caveat: 20 | - Return the desired **width** of a cell in `- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath` 21 | 22 | # To Do 23 | 24 | 1. Support `UITableViewStyleGrouped` 25 | 2. Allow section headers to push previous headers off the screen as in the vertical version 26 | 27 | # License 28 | 29 | Copyright (c) 2011 Rick Harrison, http://rickharrison.me 30 | 31 | Permission is hereby granted, free of charge, to any person obtaining a copy 32 | of this software and associated documentation files (the "Software"), to deal 33 | in the Software without restriction, including without limitation the rights 34 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 35 | copies of the Software, and to permit persons to whom the Software is 36 | furnished to do so, subject to the following conditions: 37 | 38 | The above copyright notice and this permission notice shall be included in 39 | all copies or substantial portions of the Software. 40 | 41 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 42 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 43 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 44 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 45 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 46 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 47 | THE SOFTWARE. -------------------------------------------------------------------------------- /RHHorizontalTableView.h: -------------------------------------------------------------------------------- 1 | /* 2 | * RHHorizontalTableView.h 3 | * RHHorizontalTableView 4 | * 5 | * Created by Rick Harrison on 6/28/11. 6 | * Copyright (c) 2011 Rick Harrison, http://rickharrison.me 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | * 27 | */ 28 | 29 | #import 30 | 31 | typedef enum { 32 | RHHorizontalTableViewScrollIndicatorPositionTop, 33 | RHHorizontalTableViewScrollIndicatorPositionBottom 34 | } RHHorizontalTableViewScrollIndicatorPosition; 35 | 36 | @interface RHHorizontalTableView : UITableView { 37 | id _realDataSource; 38 | id _realDelegate; 39 | RHHorizontalTableViewScrollIndicatorPosition _indicatorPosition; 40 | } 41 | 42 | @property (nonatomic) RHHorizontalTableViewScrollIndicatorPosition indicatorPosition; 43 | 44 | @end 45 | -------------------------------------------------------------------------------- /RHHorizontalTableView.m: -------------------------------------------------------------------------------- 1 | /* 2 | * RHHorizontalTableView.m 3 | * RHHorizontalTableView 4 | * 5 | * Created by Rick Harrison on 6/28/11. 6 | * Copyright (c) 2011 Rick Harrison, http://rickharrison.me 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining 9 | * a copy of this software and associated documentation files (the 10 | * "Software"), to deal in the Software without restriction, including 11 | * without limitation the rights to use, copy, modify, merge, publish, 12 | * distribute, sublicense, and/or sell copies of the Software, and to 13 | * permit persons to whom the Software is furnished to do so, subject to 14 | * the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be 17 | * included in all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 | * 27 | */ 28 | 29 | #import 30 | #import "RHHorizontalTableView.h" 31 | 32 | CGFloat const RHHorizontalTableViewScrollIndicatorPositionBottomOffset = 9.0; 33 | 34 | @interface RHHorizontalTableView () 35 | 36 | @end 37 | 38 | @implementation RHHorizontalTableView 39 | 40 | @synthesize indicatorPosition = _indicatorPosition; 41 | 42 | #pragma mark - 43 | #pragma mark Initialization 44 | 45 | - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style { 46 | if ((self = [super initWithFrame:frame style:style])) { 47 | self.transform = CGAffineTransformMakeRotation(-1 * M_PI / 2.0); 48 | self.frame = frame; 49 | } 50 | return self; 51 | } 52 | 53 | - (id)initWithCoder:(NSCoder *)aDecoder { 54 | if ((self = [super initWithCoder:aDecoder])) { 55 | CGRect _beforeRotationFrame = self.frame; 56 | self.transform = CGAffineTransformMakeRotation(-1 * M_PI / 2.0); 57 | self.frame = _beforeRotationFrame; 58 | } 59 | return self; 60 | } 61 | 62 | #pragma mark - 63 | #pragma mark Appearance Configuration 64 | 65 | - (void)setIndicatorPosition:(RHHorizontalTableViewScrollIndicatorPosition)indicatorPosition { 66 | _indicatorPosition = indicatorPosition; 67 | 68 | if (indicatorPosition == RHHorizontalTableViewScrollIndicatorPositionTop) { 69 | self.scrollIndicatorInsets = UIEdgeInsetsZero; 70 | } else if (indicatorPosition == RHHorizontalTableViewScrollIndicatorPositionBottom) { 71 | self.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, self.frame.size.height - RHHorizontalTableViewScrollIndicatorPositionBottomOffset); 72 | } 73 | } 74 | 75 | #pragma mark - 76 | #pragma mark Table View Data Source 77 | 78 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 79 | return [_realDataSource tableView:tableView numberOfRowsInSection:section]; 80 | } 81 | 82 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 83 | UITableViewCell *cell = [_realDataSource tableView:tableView cellForRowAtIndexPath:indexPath]; 84 | 85 | if (cell) { 86 | cell.backgroundView.transform = CGAffineTransformMakeRotation(M_PI / 2.0); 87 | cell.selectedBackgroundView.transform = CGAffineTransformMakeRotation(M_PI / 2.0); 88 | cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2.0); 89 | } 90 | 91 | return cell; 92 | } 93 | 94 | #pragma mark - 95 | #pragma mark Table View Delegate 96 | 97 | - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 98 | if ([_realDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)]) { 99 | return 0.1; 100 | } 101 | 102 | return 0.0; 103 | } 104 | 105 | - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 106 | if ([_realDelegate respondsToSelector:@selector(tableView:viewForHeaderInSection:)]) { 107 | UIView *headerView = [_realDelegate tableView:tableView viewForHeaderInSection:section]; 108 | 109 | CGFloat desiredHeight = CGRectGetHeight(headerView.frame); 110 | 111 | if ([_realDelegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)]) { 112 | desiredHeight = [_realDelegate tableView:tableView heightForHeaderInSection:section]; 113 | } 114 | 115 | headerView.frame = CGRectMake(0, 0, CGRectGetWidth(headerView.frame), desiredHeight); 116 | 117 | /* 118 | * Create a wrapper for the desired header view and rotate it 119 | */ 120 | 121 | UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; 122 | view.autoresizingMask = UIViewAutoresizingNone; 123 | view.transform = CGAffineTransformMakeRotation(M_PI / 2.0); 124 | 125 | [view addSubview:headerView]; 126 | 127 | return [view autorelease]; 128 | } 129 | 130 | return nil; 131 | } 132 | 133 | #pragma mark - 134 | #pragma mark Protocol/Message Forwarding 135 | 136 | - (BOOL)respondsToSelector:(SEL)aSelector { 137 | BOOL result = [super respondsToSelector:aSelector]; 138 | 139 | /* 140 | * Check if the selector is part of the UITableViewDataSource protocol. 141 | */ 142 | 143 | struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), aSelector, NO, YES); 144 | 145 | if (dataSourceMethod.name != nil) { 146 | result = [_realDataSource respondsToSelector:aSelector]; 147 | } 148 | 149 | /* 150 | * Check if the selector is part of the UITableViewDelegate protocol. 151 | */ 152 | 153 | struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), aSelector, NO, YES); 154 | 155 | if (delegateMethod.name != nil) { 156 | result = [_realDelegate respondsToSelector:aSelector]; 157 | } 158 | 159 | return result; 160 | } 161 | 162 | - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { 163 | NSMethodSignature *signature = [super methodSignatureForSelector:aSelector]; 164 | 165 | /* 166 | * Check if the selector is part of the UITableViewDataSource protocol. 167 | */ 168 | 169 | struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), aSelector, NO, YES); 170 | 171 | if (dataSourceMethod.name != nil) { 172 | signature = [(NSObject *)_realDataSource methodSignatureForSelector:aSelector]; 173 | } 174 | 175 | /* 176 | * Check if the selector is part of the UITableViewDelegate protocol. 177 | */ 178 | 179 | struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), aSelector, NO, YES); 180 | 181 | if (delegateMethod.name != nil) { 182 | signature = [(NSObject *)_realDelegate methodSignatureForSelector:aSelector]; 183 | } 184 | 185 | return signature; 186 | } 187 | 188 | - (void)forwardInvocation:(NSInvocation *)anInvocation { 189 | SEL selector = [anInvocation selector]; 190 | struct objc_method_description dataSourceMethod = protocol_getMethodDescription(@protocol(UITableViewDataSource), selector, NO, YES); 191 | struct objc_method_description delegateMethod = protocol_getMethodDescription(@protocol(UITableViewDelegate), selector, NO, YES); 192 | 193 | /* 194 | * Route the invocation to the original data source, delegate, or super 195 | */ 196 | 197 | if (dataSourceMethod.name != nil && [_realDataSource respondsToSelector:selector]) { 198 | [anInvocation invokeWithTarget:_realDataSource]; 199 | } else if (delegateMethod.name != nil && [_realDelegate respondsToSelector:selector]) { 200 | [anInvocation invokeWithTarget:_realDelegate]; 201 | } else { 202 | [super forwardInvocation:anInvocation]; 203 | } 204 | } 205 | 206 | #pragma mark - 207 | #pragma mark Overrides 208 | 209 | - (void)setDataSource:(id )dataSource { 210 | _realDataSource = dataSource; 211 | 212 | [super setDataSource:self]; 213 | } 214 | 215 | - (void)setDelegate:(id )delegate { 216 | _realDelegate = delegate; 217 | 218 | [super setDelegate:self]; 219 | } 220 | 221 | #pragma mark - 222 | 223 | - (void)dealloc { 224 | [super dealloc]; 225 | } 226 | 227 | @end 228 | --------------------------------------------------------------------------------